Click here to Skip to main content
12,751,306 members (33,671 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">
         <form name="form1" method="post" action="insert_ac.php">
         <table width="100%" border="0" cellspacing="1" cellpadding="3">
               <td colspan="3">Insert Data Into mySQL Database </td>

$host=""; // 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

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

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

// close connection
Posted 27-Nov-12 5:33am
Updated 27-Nov-12 6:31am
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
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.

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.170215.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2017
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