Click here to Skip to main content
12,290,514 members (30,253 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL HTML PHP
I have two files. insert.php and insert_ac.php

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

<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
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";
}
?>
Posted 27-Nov-12 4:33am
Edited 27-Nov-12 5:31am
v2
Comments
digimanus 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"; } ?>
ryanb31 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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try doing
echo $sql;
to see if the sql is generating the way it should. If not, you can try
$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.
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160518.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100