2

first post here and a newbie at php programming.

I am trying to insert $val(2) into mysql database. My code is as follows:

$username = "xxx";
$password = "xxx";
$hostname = "xxx";
$val= "2";

$connection=mysql_connect ($hostname, $username, $password)
or die("Unable to connect to MySQL");

$selected = mysql_select_db("database",$connection)
or die("Could not select examples");

$url = "http:xxxxxxxxxxxxx.com"; //url here
$str = file_get_contents($url);

$sql = "INSERT INTO testtable (data)
VALUE ('".$val."')";
   
$ins= mysql_query($test);
if(! $ins )
{
 die('Could not insert: ' . mysql_error());
}

When i run the code, there is no error printed out, but no data is being inserted into the table. Can someone point out the problem. I would appreciate the help.

Addendum:

I didn't include the lines:

$url = "http:xxxxxxxxxxxxx.com";    //url here
$str = file_get_contents($url);

in my first post.

The real solution is to change:

$test to $sql

and to comment out the 2 lines below

file_get_content($url) 
$str = file_get_contents($url);

These 2 lines has to be placed on top (before the $username= "xxx" line) for it to work. Apologies for the misleading question.

  • 2
    Can you try replacing `value` with `values`? – Darshan Mehta Mar 27 '16 at 22:06
  • 2
    Is it intended that you're supplying $test to mysql_query instead of $sql? – Alexa Y Mar 27 '16 at 22:07
  • @DarshanMehta I tried changing value to values, still not working –  Mar 27 '16 at 22:09
  • @BenY My mistake, it should be $sql instead of $test, unfortunately, it still isnt working –  Mar 27 '16 at 22:10
  • http://php.net/manual/en/function.mysql-error.php – Axalix Mar 27 '16 at 22:19
  • You say no error is printed, so you're saying that either it doesn't enter the if-block, or is `mysql_error()` empty? – Qirel Mar 27 '16 at 22:22
  • @Qirel I tried adding an echo statement in the if block, but it doens't echo anything. My guess it doens't enter the if block –  Mar 27 '16 at 22:25
  • You could do it in a single-line, though: `$ins = mysql_query($sql) or die(mysql_error());` -- if it doesn't die, the query should be successful. You can also `echo $sql;` to make sure your query looks alright.. And make sure that you're actually using names in the table that matches the one in your query. – Qirel Mar 27 '16 at 22:27
  • What version of PHP are you running? `mysql_` was *removed* in PHP7. If *nothing at all* is printing (when something should be printed), you have a fatal/syntax error somewhere, and you'll need to enable [`error_reporting(E_ALL);`](http://php.net/manual/en/function.error-reporting.php) and check your logs. – Qirel Mar 27 '16 at 22:30
  • @Qirel I added that line and for some reason I cannot explain, it works like a charm! Thanks a lot for the help –  Mar 27 '16 at 22:38
  • @Qirel im running php 5.4, anyways it's working now so all is fine and dandy. Once again, thanks –  Mar 27 '16 at 22:40

4 Answers4

2

You have an SQL error here:

$sql = "INSERT INTO testtable (data)
 VALUE ('".$val."')";

VALUE should be VALUES.

You should always check for errors when something does not work as intended ( mysql_error in this case).

Also, you should not use the mysql extension any more. It has been deprecated for ages. Look into either mysqli or PDO for a better and safer extension.

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
2

The fact of the matter is that you were calling the wrong variable in:

$ins= mysql_query($test);

which should have been $sql and not $test

$ins= mysql_query($sql);

as per the query:

$sql = "INSERT INTO testtable (data)
 VALUE ('".$val."')";

which both VALUES and VALUE are valid in MySQL. Some have the misconception to believe it must be VALUES. Both are valid.

Reference:

From the manual:

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name,...)] 
    [(col_name,...)]
    {VALUES | VALUE} ({expr | DEFAULT},...),(...),...
    [ ON DUPLICATE KEY UPDATE
      col_name=expr
        [, col_name=expr] ... ]

And error reporting would have told you about the undefined variable $test.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


Your present code is open to SQL injection. Use mysqli_* with prepared statements, or PDO with prepared statements.


Footnotes:

mysql_* functions deprecation notice:

http://www.php.net/manual/en/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed as of PHP 7.0. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.

Documentation for MySQL can be found at » http://dev.mysql.com/doc/.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thanks for the explanation. I changed the variable $test to $sql, but it still wasn't working. Turns out the real problem was that i inserted a file_get_contents right before the mysql_query($sql) line. This somehow messed up the whole program big time. My main goal was actually to read from a website and insert it into database, which explains the file_get_contents. Upon commenting out this line, it works. I will keep a mental note on deprecation and sql injection –  Mar 27 '16 at 23:13
  • @user You're welcome, but in not showing your full / real code, was a tad misleading, wouldn't you agree? ;-) – Funk Forty Niner Mar 27 '16 at 23:47
  • I totally agree with you, will do so next time. –  Mar 28 '16 at 00:01
  • @user In all fairness to future readers of the question/answers; you should update your question with the real code you're using. That way an actual solution would have been posted rather than being misleading/falsified where you did state here in comments that that wasn't the real problem in having used `VALUE` which I mentioned scattered here in there and under an answer that in MySQL, both `VALUE` are `VALUES` are valid http://dev.mysql.com/doc/refman/5.7/en/insert.html and the 2nd issue was that you were using the wrong variable. People may downvote some of the answers because of this. – Funk Forty Niner Mar 28 '16 at 02:28
  • I didn't think file_get _content() would have any affect which is why i didn't include it, how wrong I was. Apologies, I have modified the question to include it. –  Mar 28 '16 at 03:25
0

Try to update your INSERT method as shown below (You missed "S" at the end of "VALUE"):

$sql = "INSERT INTO testtable(data) VALUES ('".$val."')"; 

On the other hand please be aware that there are different variables on the line below and you can use "slq" instead of "test" as shown below:

$ins= mysql_query($sql);

For more information have a look at PHP Insert Data Into MySQL.

Hope this helps...

Murat Yıldız
  • 11,299
  • 6
  • 63
  • 63
  • Thanks for the help. I changed VALUE to VALUES, still not working –  Mar 27 '16 at 22:14
  • There is nothing printed on the php page, so im assuming there's no exeptions –  Mar 27 '16 at 22:28
  • I replaced the if block with this line $ins = mysql_query($sql) or die(mysql_error()); as suggested by Qirel and change VALUE to VALUES and it works! Thanks for the help –  Mar 27 '16 at 22:43
  • VALUE is acceptable in MySQL http://dev.mysql.com/doc/refman/5.7/en/insert.html `{VALUES | VALUE}` – Funk Forty Niner Mar 27 '16 at 22:49
0

$username = "xxx";
$password = "xxx";
$hostname = "xxx";
$val= "2";

$connection=mysql_connect ($hostname, $username, $password)
or die("Unable to connect to MySQL");

$selected = mysql_select_db("database",$connection)
or die("Could not select examples");

$sql = "INSERT INTO testtable (data)
 VALUES ('".$val."')";
 
$ins= mysql_query($sql);
if(! $ins )
{
 die('Could not insert: ' . mysql_error());
}