...

View Full Version : quick code check for php novice



pphill2
12-31-2007, 06:58 AM
Assuming a valid array is passed to the function and the database connection is successful does everything look ok?

code for functions.php:
---------------------


<?php

require_once('dbconnect.php');

function createset($setinfo)
{

$query = "INSERT INTO sets ('setid', 'setname', 'setdesc') VALUES ('', $setinfo['setname'], $setinfo['setdesc'])";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "Set: $setinfo['setname'] created";

}

mysql_close($link);

?>

twomt
12-31-2007, 09:17 AM
Without really testing it, I think there are some issues with the quotes you have been using and perhaps the following would work better:


<?php

require_once('dbconnect.php');

function createset($setinfo)
{

$query = "INSERT INTO sets (`setname`, `setdesc`) VALUES ('{$setinfo["setname"]}', '{$setinfo["setdesc"]}')";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "Set: {$setinfo["setname"]} created";

}

mysql_close($link);

?>

Inigoesdr
12-31-2007, 09:37 AM
Make sure you use braces around your array variables(or concatenate them) inside of a string:

echo "Set: {$setinfo['setname']} created";
// or
echo 'Set: ' . $setinfo['setname'] . ' created';
Also, to optionally take debugging a little further you can use mysql_affected_rows() (http://php.net/mysql_affected_rows) on MySQL UPDATE/INSERT/DELETEs to make sure the row was updated/inserted/deleted:

if(mysql_affected_rows() == 1) // 1 for this case; could be more with other queries
{
// echo success?
}
else
{
// error
}
And if you need the ID from the auto_increment column you can get it with mysql_insert_id() (http://php.net/mysql_insert_id).

GJay
12-31-2007, 11:11 AM
assuming setname and setdesc are strings, you'll want to put them through mysql_real_escape_string() before going anywhere near the database

pphill2
12-31-2007, 08:17 PM
Thank you, very helpful



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum