<?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.

Recommended Answers

All 17 Replies

What does the function now() do exactly ??

can you post the function here ?

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....

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

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?

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 ;)

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

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 :)

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

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:"

this is all I got it to show:

Couldn?t execute query.Query was empty

ok now the next step :P

echo $sql;

and please paste what it shows. :)

<?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.

$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];

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

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];

what part of the code is the best place to declare these at?

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.