php mysql and pdo | Newbie | | Join Date: Oct 2009
Posts: 31
| |
Hi all,
Im busy trying to move from archaic php to lovely OO php, but i have hit a hurdle.
The following two scripts connect, request data to be inserted, and is meant to store it, but it doesnt and i get no errors.
for my own sanity can you advise?
Thank you
connection script - <?php
-
$dbtype = "mysql";
-
$dbhost = "localhost";
-
$dbname = "case";
-
$dbuser = "root";
-
$dbpass = "root";
-
-
try {
-
$db = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
-
/*** echo a message saying we have connected ***/
-
echo 'Connected to database';
-
echo '<br/>';
-
}
-
catch(PDOException $e)
-
{
-
echo $e->getMessage();
-
}
-
?>
form script -
<?php
-
-
include("includes/connectpdomysql.php");
-
-
if (isset($_POST['user_username']) && isset($_POST['user_password']) && isset($_POST['user_email']))
-
{
-
-
//Prevent SQL injections
-
$user_id = mysql_real_escape_string($_POST['user_id']);
-
$user_username = mysql_real_escape_string($_POST['user_username']);
-
$user_password = md5($_POST['user_password']);
-
$user_fname = mysql_real_escape_string($_POST['user_fname']);
-
$user_surname = mysql_real_escape_string($_POST['user_surname']);
-
$user_email = mysql_real_escape_string($_POST['user_email']);
-
$user_number = mysql_real_escape_string($_POST['user_number']);
-
$user_add1 = mysql_real_escape_string($_POST['user_add1']);
-
$user_add2 = mysql_real_escape_string($_POST['user_add2']);
-
$user_county = mysql_real_escape_string($_POST['user_county']);
-
$user_postcode = mysql_real_escape_string($_POST['user_postcode']);
-
$user_type = mysql_real_escape_string($_POST['user_type']);
-
-
-
$sql = "INSERT INTO users (user_id, user_username, user_password, user_fname, user_surname, user_email, user_number, user_add1, user_add2, user_county, user_postcode, user_type) VALUES (user_id, :user_username ,:user_password, :user_fname, :user_surname, :user_email, :user_number, :user_add1, :user_add2, :user_county, :user_postcode, :user_type)";
-
$q = $db->prepare($sql);
-
$q->execute(array(':user_id'=>$user_id,
-
':user_username'=>$user_username,
-
':user_title'=>$user_password,
-
'user_fname'=>$user_fname,
-
'user_surname'=>$user_surname,
-
':user_email'=>$user_email,
-
':user_number'=>$user_number,
-
':user_add1'=>$user_add1,
-
':user_add2'=>$user_add2,
-
':user_county'=>$user_county,
-
':user_postcode'=>$user_postcode,
-
':user_type'=>$user_type));
-
-
echo "record created";
-
-
if(!$q)
-
{
-
die("Execute query error, because: ". $conn->errorInfo());
-
}
-
-
$q->setFetchMode(PDO::FETCH_BOTH);
-
-
// fetch
-
while($r = $q->fetch()){
-
print_r($r);
-
}
-
-
}
-
-
?>
-
-
-
<html>
-
-
<form action="newuser.php" method="post">
-
Username: <input name="user_username" type="text" /><br/>
-
Password: <input type="user_password" name="password" /><br/>
-
First name: <input name="user_fname" type="text" /><br/>
-
Surname: <input name="user_surname" type="text" /><br/>
-
Email: <input name="user_email" type="text" /><br/>
-
Contact number: <input name="user_number" type="text" /><br/>
-
Address line 1: <input name="user_add1" type="text" /><br/>
-
Address line 2: <input name="user_add2" type="text" /><br/>
-
County: <input name="user_county" type="text" /><br/>
-
Post Code: <input name="user_postcode" type="text" /><br/>
-
User Type: <input name="user_type" type="text" /><br/>
-
-
<input type="submit" value="Submit" /><br/>
-
-
</form>
-
-
</html>
-
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,736
| | | re: php mysql and pdo
Hey.
Do you have error messages turned on?
On lines #45 and #48 in your form script you use the $q variable as an object, but the PDOStatement::execute method only returns TRUE or FALSE.
Also, on line #38 you prematurely print a success message, before you actually verify that the record has been created successfully.
I must admit, I am not very experienced with PDO, so there may be something I am overlooking.
O, and P.S. - if (isset($var1) && isset($var2) && isset($var3))
-
// Can be written:
-
if (isset($var1, $var2, $var3))
Just to save you a couple of key-strokes in the future ;-)
| | Newbie | | Join Date: Oct 2009
Posts: 31
| | | re: php mysql and pdo
Hi,
Thanks for the response.
Ive transferred it to my live hosting and the same happens.
I enter something into all of the fields but when i click submit all of the fields empty.
| | Member | | Join Date: Jan 2009 Location: USA
Posts: 118
| | | re: php mysql and pdo
so what your trying ot do is press submit and keep the actual values that you just submitted?
you will need to do a results query to bring the data back to the screen. currently you have designed for entry not editing/processing/view.
hope this helps!
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,736
| | | re: php mysql and pdo
Ahh ok. I though you were talking about the data not being inserted into the database.
Yea, it's like wizardry says; that form is only designed to insert data and reset.
If you want it to restore the data to the fields after it is submitted, you will have to actually add the values to the fields.
Like: -
<insert type="text" name="user_name" value="<?php echo htmlentities($_POST['user_name']); ?>">
| | Newbie | | Join Date: Oct 2009
Posts: 31
| | | re: php mysql and pdo
Hi all, thanks for the responses.
I have done a little work.
Wizardry, your absolutly right about only wanting to insert data.
What i have done is to put the input form in a seperate file, so on clicking submit, it then calls the php script.
my understanding is
1. the form posts values to the "form action" file.
2. Isset takes those values and turns them into variables
3. The sql injection function is used
4. The password is MD5'd
5. The sql script inserts the values to the database.
Ive tried putting echos in but the only thing in having echoed is the "connected to database" message
connection script - <?php
-
$dbtype = "mysql";
-
$dbhost = "localhost";
-
$dbuser = "root";
-
$dbpass = "root";
-
-
try {
-
$db = new PDO("mysql:host=$dbhost;dbname=case",$dbuser,$dbpass);
-
/*** echo a message saying we have connected ***/
-
echo 'Connected to database';
-
echo '<br/>';
-
}
-
catch(PDOException $e)
-
{
-
echo $e->getMessage();
-
}
-
?>
add file html form - <html>
-
-
<form action="newuser.php" method="post">
-
Username: <input name="user_username" type="text" /><br/>
-
Password: <input type="user_password" name="password" /><br/>
-
First name: <input name="user_fname" type="text" /><br/>
-
Surname: <input name="user_surname" type="text" /><br/>
-
Email: <input name="user_email" type="text" /><br/>
-
Contact number: <input name="user_number" type="text" /><br/>
-
Address line 1: <input name="user_add1" type="text" /><br/>
-
Address line 2: <input name="user_add2" type="text" /><br/>
-
County: <input name="user_county" type="text" /><br/>
-
Post Code: <input name="user_postcode" type="text" /><br/>
-
User Type: <input name="user_type" type="text" /><br/>
-
-
<input type="submit" value="Submit" /><br/>
-
-
</form>
-
-
</html>
-
<?php
-
-
require("includes/connectpdomysql.php");
-
-
if (isset($_POST['user_username']) && isset($_POST['user_password']) && isset($_POST['user_fname'])&& isset($_POST['user_surname'])&& isset($_POST['user_email'])&& isset($_POST['user_number'])&& isset($_POST['user_add1'])&& isset($_POST['user_add2'])&& isset($_POST['user_county'])&& isset($_POST['user_postcode'])&& isset($_POST['user_type']))
-
-
-
{
-
-
//Prevent SQL injections
-
-
$user_username = mysql_real_escape_string($_POST['user_username']);
-
$user_fname = mysql_real_escape_string($_POST['user_fname']);
-
$user_surname = mysql_real_escape_string($_POST['user_surname']);
-
$user_email = mysql_real_escape_string($_POST['user_email']);
-
$user_number = mysql_real_escape_string($_POST['user_number']);
-
$user_add1 = mysql_real_escape_string($_POST['user_add1']);
-
$user_add2 = mysql_real_escape_string($_POST['user_add2']);
-
$user_county = mysql_real_escape_string($_POST['user_county']);
-
$user_postcode = mysql_real_escape_string($_POST['user_postcode']);
-
$user_type = mysql_real_escape_string($_POST['user_type']);
-
-
-
//Get MD5 hash of password
-
-
$user_password = md5($_POST['user_password']);
-
-
-
$sql = "INSERT INTO users (user_username, user_password, user_fname, user_surname, user_email, user_number, user_add1, user_add2, user_county, user_postcode, user_type) VALUES (:user_username,:user_password,:user_fname, :user_surname,:user_email, :user_number, :user_add1, :user_add2, :user_county, :user_postcode, :user_type)";
-
echo $sql;
-
$q = $db->prepare($sql);
-
$q->execute(array(':user_username'=>$user_username,
-
':user_password'=>$user_password,
-
':user_fname'=>$user_fname,
-
':user_surname'=>$user_surname,
-
':user_email'=>$user_email,
-
':user_add1'=>$user_add1,
-
':user_add2'=>$user_add2,
-
':user_county'=>$user_county,
-
':user_postcode'=>$user_postcode,
-
':user_type'=>$user_type));
-
echo $q;
-
}
-
?>
-
| | Newbie | | Join Date: Oct 2009
Posts: 31
| | | re: php mysql and pdo
atli, im fine with calling and displaying. Im just in this instance wanting to use PDO to insert into a database
| | Member | | Join Date: Jan 2009 Location: USA
Posts: 118
| | | re: php mysql and pdo
no prob.
i see and understand what your trying to do. what i dont see is a variable that is defined for:
echo $e-> {getMessage();} that method/function your calling
i see that $e is defined in the function/method. i dont know much about php still learning but i do know c/c++.
define the object your calling.
hope that helps!
| | Newbie | | Join Date: Oct 2009
Posts: 31
| | | re: php mysql and pdo
if you consider
$q is defined by it being equalled to something
$db is defined within the database class
$sql is also defined by its value
Also all of the entries are turned into variables through isset and the injection thing.
ill get there, but im stumped
| | Member | | Join Date: Jan 2009 Location: USA
Posts: 118
| | | re: php mysql and pdo
sorry ignore my last post i thought your were talking about not being able to receive your test message but you can get your test message.
i will repost once i look through the code some more
| | Newbie | | Join Date: Oct 2009
Posts: 31
| | | re: php mysql and pdo
thank you very much for your help
| | Member | | Join Date: Jan 2009 Location: USA
Posts: 118
| | | re: php mysql and pdo
no problem
try changing this: -
$sql = "INSERT INTO users (user_username, user_password, user_fname, user_surname, user_email, user_number, user_add1, user_add2, user_county, user_postcode, user_type) VALUES (:user_username,:user_password,:user_fname, :user_surname,:user_email, :user_number, :user_add1, :user_add2, :user_county, :user_postcode, :user_type)";
-
to this: -
$sql = ("INSERT INTO users (user_username, user_password, user_fname, user_surname, user_email, user_number, user_add1, user_add2, user_county, user_postcode, user_type) VALUES (:user_username,:user_password,:user_fname, :user_surname,:user_email, :user_number, :user_add1, :user_add2, :user_county, :user_postcode, :user_type)");
-
| | Newbie | | Join Date: Oct 2009
Posts: 31
| | | re: php mysql and pdo
well, after putting an echo statement on every line and adding your code change in ive fixed it.
here it is for you to look at. thanks again -
-
<?php
-
-
require("includes/connectpdomysql.php");
-
echo "hello";
-
if (isset($_POST['user_username']) && isset($_POST['user_password']) && isset($_POST['user_fname'])&& isset($_POST['user_surname'])&& isset($_POST['user_email'])&& isset($_POST['user_number'])&& isset($_POST['user_add1'])&& isset($_POST['user_add2'])&& isset($_POST['user_county'])&& isset($_POST['user_postcode'])&& isset($_POST['user_type']))
-
-
echo "hello";
-
{
-
-
//Prevent SQL injections
-
$user_id = '';
-
$user_username = mysql_real_escape_string($_POST['user_username']);
-
$user_password = mysql_real_escape_string($_POST['password']);
-
$user_fname = mysql_real_escape_string($_POST['user_fname']);
-
$user_surname = mysql_real_escape_string($_POST['user_surname']);
-
$user_email = mysql_real_escape_string($_POST['user_email']);
-
$user_number = mysql_real_escape_string($_POST['user_number']);
-
$user_add1 = mysql_real_escape_string($_POST['user_add1']);
-
$user_add2 = mysql_real_escape_string($_POST['user_add2']);
-
$user_county = mysql_real_escape_string($_POST['user_county']);
-
$user_postcode = mysql_real_escape_string($_POST['user_postcode']);
-
$user_type = mysql_real_escape_string($_POST['user_type']);
-
-
-
//Get MD5 hash of password
-
-
//$user_password = md5($_POST['user_password']);
-
-
-
$sql = ("INSERT INTO users (user_id, user_username, user_password, user_fname, user_surname, user_email, user_number, user_add1, user_add2, user_county, user_postcode, user_type) VALUES (:user_id, :user_username,:user_password,:user_fname, :user_surname,:user_email, :user_number, :user_add1, :user_add2, :user_county, :user_postcode, :user_type)");
-
-
$q = $db->prepare($sql);
-
$q->execute(array(':user_id'=>$user_username,
-
':user_username'=>$user_username,
-
':user_password'=>$user_password,
-
':user_fname'=>$user_fname,
-
':user_surname'=>$user_surname,
-
':user_email'=>$user_email,
-
':user_number'=>$user_number,
-
':user_add1'=>$user_add1,
-
':user_add2'=>$user_add2,
-
':user_county'=>$user_county,
-
':user_postcode'=>$user_postcode,
-
':user_type'=>$user_type));
-
}
-
?>
-
| | Member | | Join Date: Jan 2009 Location: USA
Posts: 118
| | | re: php mysql and pdo
no problem glad i could help
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: php mysql and pdo
just a notice: if you use prepared statements, you don’t need mysql_real_escape_string(). It’s the prepared statement itself, that makes up for the security (besides that even mysql_real_escape_string() can be hacked).
| | Newbie | | Join Date: Oct 2009
Posts: 31
| | | re: php mysql and pdo
so i could just get rid and have the variable = $_POST?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: php mysql and pdo
if you’re using the prepared statements, yes. (you should disable magic_quotes_gpc, tho)
mind, that this will work against SQL Injection, not HTML/Script Injection (but that’s also true for mysql_real_escape_string())
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|