user6121920
user6121920

Reputation:

Insert into mysql not working

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.

Upvotes: 2

Views: 4248

Answers (4)

Fahim Siddiqui
Fahim Siddiqui

Reputation: 20

$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());
}

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74232

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/.

Upvotes: 2

Murat Yıldız
Murat Yıldız

Reputation: 12070

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...

Upvotes: 0

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

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.

Upvotes: 2

Related Questions