0
<?php
if (isset ($_POST['chk8'])) {
$req1 = "Service Protection Plan (SPP)\n";
} else {
$req1 = "None\n";
}
if (isset ($_POST['chk9'])) {
$req2 = "Three Product Welcome Kit (Scientific Atlanta)\n";
} else {
$req2 = "None\n";
}
if (isset ($_POST['chk10'])) {
$req3 = "Three Product Welcome Kit (Motorola)\n";
} else {
$req3 = "None\n";
}
if (isset ($_POST['chk11'])) {
$req4 = "Channel Guide Magazine\n";
} else {
$req4 = "None\n";
}
if (isset ($_POST['chk12'])) {
$req5 = "Channel Line Up\n";
} else {
$req5 = "None\n";
}
if (isset ($_POST['chk13'])) {
$req6 = "Identity Theft Form\n";
} else {
$req6 = "None\n";
}
if (isset ($_POST['chk14'])) {
$req7 = " \n";
} else {
$req7 = "None\n";
}
?>
<?php
$DBHost = "db1.thinkhost.com";
$DBuser = "jaydude23";
$DBpass = "killough";
$DBname = "jaydude23";
$table =  "twc";

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBname") or die("Unable to select database $DBname");

$sqlquery = "INSERT INTO $table VALUES(now(),'$CSP_Name','$Supervisor','$Rep_Number','$Department','$Email','$req1','$req2','$req3','$req4','$req5','$req6','$req7','$Account_No','$First_Name','$Last_Name','$Phone','$ShipTo_Address','$ShipTo_City','$ShipTo_State','$ShipTo_ZIP','$Additional_Info','$Accept')";

$results = mysql_query($sqlquery);

mysql_close();

print "<html><body><center>";
print "<p>Your request was added sucessfully<p>";
print "</body></html>";
?>

I have a form on my website, which a user fills out, and then clicks 'submit'. This php code is supposed to take the data entered on the form and added it to a mysql database.

Its not working, no record is entered, all it says is "Your request was added sucessfully". What is wrong here?? Thanks in advance for all your help.

3
Contributors
17
Replies
18
Views
8 Years
Discussion Span
Last Post by ultra vires
0
LINE 40-
$sqlquery = "INSERT INTO $table VALUES(now(),'$CSP_Name','$Supervisor','$Rep_Number','$Department','$Email','$req1','$req2','$req3','$req4','$req5','$req6','$req7','$Account_No','$First_Name','$Last_Name','$Phone','$ShipTo_Address','$ShipTo_City','$ShipTo_State','$ShipTo_ZIP','$Additional_Info','$Accept')";

Looks like you did not specify the columns.

$sql = "INSERT INTO $TABLE (column1, column2, etc) ";
$sql .= "(value1, value2, etc...) ";

also you might want to add in error checking:

$result = mysql_query($sql);


if (!$result){
die('Error: Data not saved! ERROR!?' );
}echo "Success";

Hope that helps....

Edited by happygeek: fixed formatting

0

^^ You can enter data into the tables without specifying the column names if you enter in the default order of the columns :)

0

The first field in my table is a date field, I want to capture the date that the user submits the information on. Someone told me to use now() to do that. Is this not correct?

0

Yea, but I have no idea if they were entered in the order or what they are called. Guess i could have jsut said make sure they are correct ;)

0

I have verified that they are being entered in the same order as they are listed in the table.

0

now() is not a function in php.

You can use time() to get the UNIX timestamp to get the current time of the system.

time() - will return an integer value you can convert it to a more user understandable format by using the date function "date()"

Example :
date("F j, Y, g:i a", time());

Note : its better to store time in UNIX timestamp format, just make sure the field is integer type and of size 12 :)

0

For now, I have taken the the 'now()' out. It still will not add the info to the table. What am I missing here?

0

Modify this

$result = mysql_query($sql) ;

as

$result = mysql_query($sql) or die("Couldn?t execute query." . mysql_error());
echo "Result variable shows: ".$result;

and paste whats written in your browser after "Result variable shows:"

0

this is all I got it to show:

Couldn?t execute query.Query was empty

0
<?php
if (isset ($_POST['chk8'])) {
$req1 = "Service Protection Plan (SPP)\n";
} else {
$req1 = "None\n";
}
if (isset ($_POST['chk9'])) {
$req2 = "Three Product Welcome Kit (Scientific Atlanta)\n";
} else {
$req2 = "None\n";
}
if (isset ($_POST['chk10'])) {
$req3 = "Three Product Welcome Kit (Motorola)\n";
} else {
$req3 = "None\n";
}
if (isset ($_POST['chk11'])) {
$req4 = "Channel Guide Magazine\n";
} else {
$req4 = "None\n";
}
if (isset ($_POST['chk12'])) {
$req5 = "Channel Line Up\n";
} else {
$req5 = "None\n";
}
if (isset ($_POST['chk13'])) {
$req6 = "Identity Theft Form\n";
} else {
$req6 = "None\n";
}
if (isset ($_POST['chk14'])) {
$req7 = " \n";
} else {
$req7 = "None\n";
}
?>
<?php
$DBHost = "db1.thinkhost.com";
$DBuser = "jaydude23";
$DBpass = "killough";
$DBname = "jaydude23";

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBname") or die("Unable to select database $DBname");

$sqlquery = "INSERT INTO twc VALUES('$CSP_Name','$Supervisor','$Rep_Number','$Department','$Email','$req1','$req2','$req3','$req4','$req5','$req6','$req7','$Account_No','$First_Name','$Last_Name','$Phone','$ShipTo_Address','$ShipTo_City','$ShipTo_State','$ShipTo_ZIP','$Additional_Info','$Accept')";

$result = mysql_query($sql) or die("Couldn?t execute query." . mysql_error());
echo "Result variable shows: ".$result;
echo $sql;


mysql_close();
?>

Maybe I'm doing something wrong here, but I can't get it to show anything but 'couldnt execute query' line.

0

$CSP_Name,$Supervisor,$Rep_Number,$Department,$Email

Where have you set these variables in your script ??

coz i don't see any assignment, if you have not done that then please assign the values to the variables :)

for ex:
$csp_name = $_POST[cspname];

0

They come from the form that the user fills out. Do I need to declare them all in my script?

0

Yes, PHP doesnt automatically get the values from the user filled form you have to specify.

How to get values:

HTML :
<input type=text name=csp_n>

to get values in PHP you have to use :

$csp_name = $_POST[csp_n];

0

just before you use them ( its just a personal preference).

NOTE :
it is advisable to parse the variables as they can inject SQL statements.
( i wont explain the details here (as it is not in the scope of this thread), you can google for more info .) and find a quick and easy function for that too :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.