Forum Moderators: coopster

Message Too Old, No Replies

php - mysql error

         

adamnichols45

1:26 pm on Nov 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not so much an error but im trying to add data to a mysql databse.

<input type="text" name="firstname">

next page looks like this

<?php
//connect to MySQL
$connect = mysql_connect("hosting.com", "username", "password")
or die ("error.");

//make sure we're using the right database
mysql_select_db("no2");

//insert data into "details" table
$insert = "INSERT INTO details (firstname) " .
"VALUES ('".$_POST['firstname']."')";
$results = mysql_query($insert)
or die(mysql_error());
?>

there is no error shown and when i view the databse a record has been added but no data inside!

Any ideas on this please?

omoutop

2:40 pm on Nov 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



have you validated that $_POST holds any data at all?

try this simple thing to the page that accepts the data
echo "<pre>";
print_r($_POST);
echo "</pre>";

also make sure that your form is actually on method=post

barns101

2:40 pm on Nov 14, 2006 (gmt 0)

10+ Year Member



Echo $insert to see if it's getting the data from the POST array, because that's your likely issue. I would change the code as follows:

//insert data into "details" table
$insert = "INSERT INTO details (firstname) VALUES ('$_POST[firstname]')";
$results = mysql_query($insert) or die(mysql_error());

mcibor

10:03 pm on Nov 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Barns, it won't work:

either

$insert = "INSERT INTO details (firstname) VALUES ('{$_POST[firstname]}')";
$results = mysql_query($insert) or die(mysql_error());

or
$insert = "INSERT INTO details (firstname) VALUES ('".$_POST[firstname]."')";
$results = mysql_query($insert) or die(mysql_error());

and yes do
echo "insert: $insert";
that could explain, why you are having problems.

Echo and print_r are the most popular debugging tools I use ;)
Michal

adamnichols45

11:12 pm on Nov 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ok i created a new form

<form action="finished.php" method="post" name="cards" id="cards">
<input type="text" name="email2">
</form>

on the next page finished.php

<?php echo $_POST['email2'];?>

There is no data displayed on the page just a blank screen!

adamnichols45

11:26 pm on Nov 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i changed the entire form tag and it now works

<form name="Form1" method="post" action="finished.php" id="Form1">

mcibor

5:30 pm on Nov 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So just putting the name first resolved the issue?

Strange!

But can be true.

Have fun while php programming
Michal