View Single Post
Old 08-07-2012, 07:28 PM   PM User | #9
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
Hi mlseim,

Thanks again

There is a error when processing the script, all the fields expect the petathome fields are filled.

The error is:
Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /database/test_post.php on line 17
Warning: Invalid argument supplied for foreach() in /database/test_post.php on line 22

The html code is:
PHP Code:
<!DOCTYPE html>
<html>
<body background="blue">
<form action="test_post.php" method="post">

Full Name: <input name="FullName" type="text" id="FullName" /><br />
<br />
Number: <input name="Number" type="text" id="Number" /><br />
<br />
Email Address: <input name="EmailAddress" type="text" id="EmailAddress" /><br /><br />
<br/>
Address: <input name="Address" type="text" id="Address" /><br />
<br />
Which animal do you want to keep?<br />
<input type="checkbox" name="petathome[]" value="Dog" />Dog<br />
<input type="checkbox" name="petathome[]" value="Cat" />Cat<br />
<input type="checkbox" name="petathome[]" value="Lion" />Lion<br />
<input type="checkbox" name="petathome[]" value="Tiger" />Tiger<br />
<input type="checkbox" name="petathome[]" value="Leopard" />Leopard<br />
Also: <input type="text" name="petathome[]"><br />
<input type="submit" name="submit" value="Submit" />

</form>
</body>
</html>  

The full php code is:
<?php
$host
="host"// Host name 
$username="username"// Mysql username 
$password="password"// Mysql password 
$db_name="dbname"// Database name 
$tbl_name="tblname"// Table name

// Connect to server and select database.
mysql_connect("$host""$username""$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$Name=mysql_real_escape_string($_POST['Name']); 
$Number=mysql_real_escape_string($_POST['Number']); 
$EmailAddress=mysql_real_escape_string($_POST['EmailAddress']); 
$Address=mysql_real_escape_string($_POST['Address']); 
$petathome=mysql_real_escape_string($_POST['petathome']);  

// Loop through the array of checked box values ...
$pets="";
$flag=0;
foreach(
$petathome as $entry){
$pets .= $entry.", ";
$flag=1;
}
if(
$flag==1){
$pets=rtrim($pets);
}

// Insert data into mysql
$sql="INSERT INTO $tbl_name(Name, Number, EmailAddress, Address, petathome)VALUES('$Name', '$Number', '$EmailAddress', '$Address', '$pets')"
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo 
"Successful";
echo 
"<BR>";
echo 
"<a href='testpage.html'>Back to main page</a>";
}

else {
echo 
"ERROR";
}
?>

<?php
// close connection
mysql_close();
?>

Last edited by Inigoesdr; 08-09-2012 at 02:50 AM..
mohammedsali is offline   Reply With Quote