Wednesday, 15 February 2012

PHP mysql insert query not working but not giving any error messages -



PHP mysql insert query not working but not giving any error messages -

i have verified post method working using echo display variables, when utilize insert query below not add together row.

do have ideas?

<?php // 1. create db connection $connection = mysql_connect("localhost","root","p@ssword"); if(!$connection){ die("database connection failed: " . mysql_error()); } ?> <?php $menu_name = $_post['menu_name']; $position = $_post['position']; $visible = $_post['visible']; ?> <?php $query = "insert subjects (menu_name, position, visible) values ('{$menu_name}', {$position}, {$visible})"; $result = mysql_query($query, $connection); if ($result){ header("location:staff.php"); exit; } else { echo "<p> there error when creating subject </p>"; "<p>". mysql_error()."</p>" ; }

?>

confusing code o.o!. been long time since code looked this. hard figure out, lol. but, when comes mysql errors - when not providing me error (when debugging looks this) - read line line. echo query, , test mysqladmin or other sql tool. run $query = mysql_query($query) or die(mysql_error()); on same line quick debugging.

notable issues

a) switch mysqli ready future php changes. familiar mysqli if mysql.

you didn't select database. mysql_select_db important it's post info - means, need filter information. allow entering of database using insert, without escaping unsafe characters - you're leaving lot play ;). even, if you're 1 entering info changed bit below. have '{$menu_name}' single quotes, {$position} , {$visible} without single quotes - inconsistent confusing when go months later.

why ('{$menu_name}', {$position}, {$visible}) not ('{$menu_name}', '{$position}', '{$visible}') ? instead.

more organized <? $host = "localhost"; // hostname $user = "root"; // username $pass = "p@ssword"; // password $db = ""; // database name $connection = mysql_connect($host,$user,$pass) or die("database connection failed: ".mysql_error()); $database = mysql_select_db($db,$connection) or die("db selection error: ".mysql_error()); $menu_name = mysql_real_escape_string($_post['menu_name']); $position = mysql_real_escape_string($_post['position']); $visible = mysql_real_escape_string($_post['visible']); $query = "insert `subjects` (`menu_name`, `position`, `visible`) values ('{$menu_name}', '{$position}', '{$visible}')"; $result = mysql_query($query, $connection) or die(mysql_error()); if ($result){ // utilize mysql_insert_id validation auto_increment tables. header("location:staff.php"); exit; } else { echo "<p> there error when creating subject </p> <p>". mysql_error()."</p>" ; } ?>

php mysql forms insert-update

No comments:

Post a Comment