By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,053 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,053 IT Pros & Developers. It's quick & easy.

simple adding of data to a table

P: n/a
Hi,
I'm basically creating a system which add records to a pre made SQL
database built in SQLyog.
So far i'va had no problems actually connecting to the database and
displaying resulst but I just cannot make it add records. I know its
connected to the database as a I made a mistake with connection
details on purpose to see if it recognised it.
I just add data into a text field, click submit and the page goes to
a
new, blank page with no changes to the table when I check in SQLyog.
Any help would be much appreciated, as I am very new to PHP.
Thanks Tom.

Here is the code I have tried (simple add before I code the proper
system):
HTML page
<form action="script.php" method="post">
Test: <input type="text" name="test2"><br>
<input type="Submit">
</form>
and the PHP script to add data:
<?
$host="localhost"
$username="root";
$database="test";
$test2=$_POST['test2'];
mysql_pconnect($host,$username);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO test2 VALUES ('','$test2')";
mysql_query($query);
mysql_close();
?>

Feb 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Rik
<to**********@hotmail.comwrote:
So far i'va had no problems actually connecting to the database and
displaying resulst but I just cannot make it add records.
$test2=$_POST['test2'];
$query = "INSERT INTO test2 VALUES ('','$test2')";
mysql_query($query);
Well, let's check why mysql doesn't like it:
mysql_query($query) or die("Error in query:\n$query\nMySQL
sais:\n".mysql_error());

--
Rik Wasmus
Feb 7 '07 #2

P: n/a
..oO(to**********@hotmail.com)
>and the PHP script to add data:
<?
<?php
>$host="localhost"
$username="root";
root?! You should create and use a normal user account.
>$database="test";
$test2=$_POST['test2'];
Check with isset() if $_POST['test2'] exists.
>mysql_pconnect($host,$username);
mysql_connect(...);
>@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO test2 VALUES ('','$test2')";
mysql_query($query);
No error handling. What does mysql_query() return? Or mysql_error()?
Is error_reporting set to E_ALL?

Micha
Feb 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.