Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a query is inserted from PHPMYAdmin but not from PHP

Tags:

php

mysql

i'm writing a php code to insert form values in a forum values

$dbServer = mysql_connect("localhost" , "root", "") ; 
if(!$dbServer) die ("Unable to connect");
mysql_select_db("kfumWonder");
$name= $_POST['name'] ; 
$password= md5($_POST['password']); 
$email= $_POST['email'] ; 
$major= $_POST['major'] ; 
$dateOfBirth=$_POST['dateOfBirth'] ; 
$webSite = $_POST['website']; 
$joinDate= date("Y m d") ;

$query = "INSERT INTO user (name, password, email, major, dob, website, join_date)
          Values ('$name', '$password', '$email', '$major', '$dateOfBirth',
                  '$webSite' , '$joinDate')" ; 

//echo $query ; 
$result = mysql_query($query) ;

if (! $result ) 
 echo " no results "  ;

this works perfectly fine when i took the printed query and run it in PHPMyAdmin but when i run this code nothing happens

like image 584
Iyad Al aqel Avatar asked Nov 15 '22 04:11

Iyad Al aqel


1 Answers

Your POST vars need to be escaped if you do not have magic quotes on like this mysql_real_escape_string($_POST['blah']). Even if magic quotes is on, you should strip slashes, or turn off magic quotes in the cofig, and re-escape them with mysql_real_escape_string. Or use PDO to do database entries as it handles this for you.

Also, to see what your errors are, you could call your query like this:

if (!$result = mysql_query($query)) echo mysql_error();
like image 123
dqhendricks Avatar answered Dec 24 '22 04:12

dqhendricks