Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySql_query insert doesn't work

Status
Not open for further replies.

Elmserv

Programmer
Sep 25, 2004
72
GB
Can anyone see why this chunk of code doesn't work.
No parsing errors, the record just doesn't appear in the table


*-*
<?php
include('functions.php');

if (strlen(trim($_POST['name'])) == 0)
{
Print 'Name required';
return;
}
if (strlen(trim($_POST['mobile'])) == 0)
{
Print 'Mobile number required';
return;
}
if (strlen(trim($_POST['van'])) == 0)
{
Print 'Van details required';
return;
}


// open connection
$cMysql = conectmysql();


// check connection
if (mysql_errno())
{
die("Can't connect to SQL server");
return;
}

// select database
$res = mysql_select_db('Pcouriers', $cMysql);


// format variables
$name = strtoupper(trim($_POST['name']));
$address = strtoupper(trim($_POST['address']));
$mobtel = trim($_POST['mobile']);
$van = strtoupper(trim($_POST['van']));

// build query
$query = "INSERT INTO `drivers` (name, mobtel, vantype, address) VALUES ($name, $mobtel, $van, $address)";

// insert data
$res = mysql_query($query, $cMysql);
mysql_close($cMysql);

// nothing
print $res;


return;
?>


*-*
 
i'm not convinced of this control
Code:
if (mysql_errno())
it will probably work but it is not (imo) good practice because you are effectvely (implicitly) casting an integer to a boolean.

i would rather see an explicit comparison:
Code:
if (mysql_errno() !== 0)

alternatively, if your connection script returns false why not just check for
Code:
if(!$cMysql)

lastly (and this is the reason your code is not working)

you should escape and enquote values before entering them into a sql query. try the following instead:
Code:
$query = "
  INSERT INTO drivers 
  (`name`, `mobtel`, `vantype`, `address`) 
  VALUES 
  (
    '".mysql_real_escape_string($name)."', 
    '".mysql_real_escape_string($mobtel)."', 
    '".mysql_real_escape_string($van)."', 
    '".mysql_real_escape_string($address)."'
  )";

$res = mysql_query($query);
if (!$res)
{
  die("Something wrong.  Query was <br/>$query<br/> and mysql error was <br/>".mysql_error());
} 
else 
{
  echo "query worked fine";
}

you would have picked up on this had you had error reporting turned on. for debugging i recommed executing the following library function at the top of your scripts
Code:
error_reporting(E_ALL);
and make sure that display_errors is turned on in your php.ini file.
 
Thats fantastic, I eventually worked out about the quotes but the error reporting string is very usefull.

I actually picked up the "if (mysql_errno())" from a book I had bought (I don't want to mention names litigation is to expensive)

Thanks


Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top