Click here to Skip to main content
15,909,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two files. insert.php and insert_ac.php

But I can't sotre the data into sql server.

PHP
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
   <tr>
      <td>
         <form name="form1" method="post" action="insert_ac.php">
         <table width="100%" border="0" cellspacing="1" cellpadding="3">
            <tr>
               <td colspan="3">Insert Data Into mySQL Database </td>
            </tr>
            <tr>
               <td width="71">Name</td>
               <td width="6">:</td>
               <td width="301"><input name="name" type="text" id="name"></td>
            </tr>
            <tr>
               <td>Lastname</td>
               <td>:</td>
               <td><input name="lastname" type="text" id="lastname"></td>
            </tr>
            <tr>
               <td>Email</td>
               <td>:</td>
               <td><input name="email" type="text" id="email"></td>
            </tr>
            <tr>
               <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
            </tr>
         </table>
         </form>
      </td>
   </tr>
</table>

PHP
<?php
insert_ac.php
$host="192.168.1.2"; // Host name
$username="xyz"; // Mysql username
$password="abc"; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // 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=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname', '$email')";
$result=mysql_query($sql);

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

<?php
// close connection
mysql_close();
?>
Posted
Updated 27-Nov-12 5:31am
v2
Comments
Herman<T>.Instance 27-Nov-12 10:58am    
what error do you get? does the table $tbl_name really exist in your DB? I doubt that
have you tried:
$sql=sprintf("INSERT INTO %s (name, lastname, email) VALUES ('%s', '%s', '%s')", $tbl_name, $name, $lastname, $email);
Member 9380535 27-Nov-12 11:06am    
Yes the table $tbl_name already exist. and I tried your query and got the same error :

"; echo "Back to main page"; } else { echo "ERROR"; } ?>
ZurdoDev 27-Nov-12 11:34am    
I don't do php but it does not look like you are injecting the values of $name and $lastname into $sql. They appear to be in a single string "". Which means $name is literally getting written to the db. I could be wrong, and if so, just ignore. :)

1 solution

Try doing
PHP
echo $sql;
to see if the sql is generating the way it should. If not, you can try
PHP
$sql="INSERT INTO ".$tbl_name."(name, lastname, email)VALUES('".$name."', '".$lastname."', '".$email."')";

If the sql seems right, try copying it and running it on sql mannager to see if you get an error. Aparently it's an error from your sql server and not php so try that.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900