Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Record not being added to table

Status
Not open for further replies.

dbaseboy

Programmer
Apr 24, 2002
48
GB
Hi all,

just started to try and teach meself this thing for the website so I thought Id have a go at a ridiculously easy calendar thing but no matter what I do or how much I google I cant suss whats up.

Heres what I have so far:

A MySQL database called peasepu_whowhere which contains 1 table called items which in turn has 4 fields called who, when, loc & killby


A file called testphp.php which is

Code:
 <html>
<head><title>Get data</title></head>
<body>
<?php
 $dbh=mysql_connect ("localhost",myusername,mypassword) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("peasepu_whowhere");
?>

<form action="insert.php" method="post">
Who: <input type="text" name="person"><br>
Where: <input type="text" name="place"><br>
When: <input type="text" name="datethere"><br>
Kill date: <input type="text" name="leavetill"><br>
<input type="Submit">
</form>
</body>
</html>

I know the connection is working because if I change the myusername slightly I get a cant connect error.


and then insert.php containing :

Code:
 <html>
<head><title>Insert a record</title></head>
<body>
<?php
$per=$_POST['person'];
$place=$_POST['place'];
$datethere=$_POST['datethere'];
$leavetill=$_POST['leavetill'];

$sql = "INSERT INTO `items` (`who`, `when`, `loc`, `killby`) VALUES ($per, $place,$datethere, $leavetill )";
mysql_query($sql);
?>

</body>
</html>

I dont get any error messages or anything strange, just nothing happens, no records are added to the table.
 
You should move your database connection and database selection scripts into your processing script (insert.php) not the one that submits the information. Since http is discontinuous, you're no longer connected to the database when the second script runs.
 
Thanks, done that but still no joy :(

Exactly the same, appears to work but no update to the table.
 
Looks to me you're inserting strings rather than integers. If you're doing that, you need surround them with quotes:
Code:
$sql = "INSERT INTO `items` (`who`, `when`, `loc`, `killby`) VALUES ('$per', '$place', '$datethere', '$leavetill')";
 
tried with no quotes, single quotes, double quotes, that little quote that sits below the Esc key, square brackets etc etc all to no avail. I've tried just hard coding the data in the insert.php in case its the form at fault.

You name it, I've tried it.
 
Do you get any error if you add a check?
Code:
mysql_query($sql) or die ('Error in executing query: ' . mysql_error());
How are the permissions? Is the above user allowed to insert new records in the database?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top