0

I have a db connection and want to test inserting a variable value into a db field. It's not inserting anything but cannot see why...can anyone spot the problem please?

Updated - here's all the php code:

    //DB Conn

<?php
    $link = mysql_connect('localhost', 'root', '');
    if (!$link) {
    die('Could not connect: ' . mysql_error());
}
    mysql_select_db('mydb',$link);
?>

    ... then
    //Save.php

    <?php

    include('dbconn.php');

    $result = mysql_query($query, $link);

        $mytestvalue = 'hello';

        $query="insert into mytable (id, name) values ('null','".$mytestvalue ."')";

    ?>

Any ideas?

5
  • Because you're never actually executing that query…? Commented Jul 30, 2011 at 8:54
  • Where's the code where you perform the query? Commented Jul 30, 2011 at 8:55
  • 2
    'null' is not NULL, it's literally the word null... Commented Jul 30, 2011 at 8:57
  • 1
    @DanGrossman means it should look like this: $query="insert into mytable (id, name) values (NULL,'".$mytestvalue ."')";. However if ID is a auto_increment primary key then there is no need to even pass in NULL you can just leave ID out of the insert query for the same effect. Commented Jul 30, 2011 at 9:01
  • 1
    You should do corrections in code you actually run, but It is better to leave the code in your question intact. People have pointed you miss a call to mysql_query($query, $link); and you put it in (and in the wrong place), now the answer looks irrelevant. Commented Jul 30, 2011 at 10:10

2 Answers 2

2

You are missing the actual call to mysql_query() in your code that actually executes the SQL you have created.

$result = mysql_query($query, $link);

Add the line above to the end of the code sample in your question.

Please see this example on the PHP manual for a fully worked example of an interaction with MySQL from PHP.

Sign up to request clarification or add additional context in comments.

Comments

1

Mysql_query function should be after you define the actual query string.

So, you first define the $query and THEN execute the mysql_query function call.

Cheers!

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.