0

I am having an issue with a MySQL query as follows:

My script generates this as an example query:

INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('Test2', '123-456-7890', '[email protected]', 'mesa', 'az', '04-14-2013')

Which if I drop directly into PHPMyA, works fine. However, the PHP script I am trying to use to send the query from my website is not working and I can't get it figured out. Here it is:

$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";

mysql_query($sql);

$result = mysql_query($sql);

if($result)
{
    echo("<br>Data Input OK");
} 
else
{
   echo("<br>Data Input Failed");
}

Nothing makes it to the MySQL DB and no PHP errors are displayed, however, if I echo $sql I get the exact query I posted previously.

4
  • 2
    Did you print your query to see whether all the expected values are there? Also just a note you are executing your query two times, so once you get to fix it, it will be doing 2 inserts per run Commented Apr 14, 2013 at 13:31
  • 1
    you're running the query twice, your second attempt might be the one that's failing Commented Apr 14, 2013 at 13:31
  • why not just print(mysql_error()); Commented Apr 14, 2013 at 13:34
  • 2
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Apr 14, 2013 at 13:41

8 Answers 8

1

Just remove the single line mysql_query($sql); on your code and you will be fine.. But you should better start practicing PHP MySQLi which stands for PHP MySQL Improved, such:

$con = mysqli_connect($host, $user, $password, $password);

$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";

$result = mysqli_query($con, $sql);

if($result) {
    echo("<br>Data Input OK");
} else {
    echo("<br>Data Input Failed");
}
Sign up to request clarification or add additional context in comments.

1 Comment

problem cannot be twice usage of mysql_query($sql).
1
$sql = 'INSERT INTO Table_name (`id`, `name`) VALUES ("1", "php");

Comments

0

You are executing $sql twice in your script wich is causing the error, please remove

mysql_query($sql);

And it will be ready to go

I would also suggest to stop using mysql_query please switch to mysqli or PDO

1 Comment

Once cleaned up to only run the query once, and $result = mysql_query($sql) or die(mysql_error()); is added I am greeted with the message "No database selected" which I find perplexing since I am staring at my mysql_select_db line... Not sure where the error is, but its time to backtrack my way through the connection string
0

Are you sure there is a valid connection (..mysql_connect())? Try using the full syntax like so..

$conn = mysql_connect(...);
$result = mysql_query($query, $conn);

Also try forcing a commit after you execute the statement -

$mysql_query("COMMIT", $conn);

Comments

0

You are running mysql_query twice. Reason of the error. Try running the following code.

$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";

$result = mysql_query($sql) or die(mysql_error());

if($result){
    echo("<br>Data Input OK");
} else{
    echo("<br>Data Input Failed");
}

3 Comments

Agree but it's not the main issue here
Query being run twice is not the error OP is asking about, they don't see data in there for even one run of the query
Try debugging the code by using $result = mysql_query($sql) or die(mysql_error()); Check and see what causes the problem.
0

use this

"INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$_POST[name]', '$_POST[phone]', '$_POST[email]', '$_POST[city]', '$_POST[state]', '$_POST[date]')";

Comments

0

Try to use mysql_query($sql,$con); instead of mysql_query($sql);.

Comments

0
if(isset($_POST['submit']))
{
   $name=$_POST['name'];
   $age=$_POST['age'];
   $address=$_POST['address'];

   $ins="insert into table_name(`name`,`age`,`address`)values('".$name."','".$age."','".$address."')";
   mysql_query($ins);
   echo 'data inserted successfully';
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.