Gideon
Gideon

Reputation: 1886

Using PHP to INSERT a defined variable into MySQL DB/table, no data is (apparantly) being written to DB?

Thanks for taking a look:

Here is the php I'm using to insert the data into the table

<?php

session_start();

//sets a variable from a session value
if (isset($_SESSION['sv_01'])) {$sv_01=$_SESSION['sv_01'];} else {$sv_01="";}

//to test that the variable has been set and is not empty
echo $sv_01;

//define database log in stuff
$username="username123";
$password="password123";
$database="database01";
$table="my_table";
$dbaddress="123.123.123.123";

//connect to dbserver
$con=mysql_connect($dbaddress,$username,$password); 

if (!$con) 
{ 
die('Could not connect:' .mysql_error()); 
} 

//select the db
mysql_select_db($database) or die( "Unable to select database"); 

//insert data from variables
mysql_query("INSERT INTO $table 
(
$sv_01
)
VALUES 
(
'$sv_01'
)");

mysql_close($con);

?>

I run this, and then go to check out the contents of the DB. Using MySQL workbench I open the connection and the database and table in question, select all rows and there is no data contained in the table.

MySQL info: Collation: latin1 - default collation Engine: MyISAM datatype: sv_01 VARCHAR (255) default: NULL

Any ideas what I am doing incorrectly?

Upvotes: 1

Views: 6506

Answers (2)

Nir Alfasi
Nir Alfasi

Reputation: 53525

I believe that the name of the field is sv_01 not $sv_01

I would try:

$query = "INSERT INTO $table (sv_01) VALUES ('$sv_01')";

Update (dedicated to tadman):
A small piece of advice: DO NOT use mysql_query

Upvotes: 5

Use localhost insted af your IP (if possible), and make your connection easy to read:

$con=mysql_connect($dbaddress,$username,$password) OR DIE mysql_error();

AND you also have to give you mysql_query a variable:

$mysql = mysql_query("INSERT INTO $table ($sv_01) VALUES ('".$sv_01."');");

:)

Upvotes: 0

Related Questions