Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-17-2013, 06:53 PM   PM User | #1
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
Mysqli Update Help

What am i doing wrong?

PHP Code:
$mysqli = new mysqli("localhost""user""pass""db");
if (!
$mysqli) {
$mysqli=("UPDATE `home` SET `text`='$text' WHERE `home_id`='$home_id'");
}
mysqli_query($mysqli); 
Thanks in advance.


Slayer.
SlayerACC is offline   Reply With Quote
Old 02-17-2013, 08:01 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You're overwritting the $mysqli object with a string. I'm also not sure what you are trying to do with the if; mysqli will return an object even if you failed to connect, so you want to use mysqli_connect_error/errno to deal with connection errors.

Procedural mysqli_query will also require two arguments, the first being the mysqli object and the second being the string to execute.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-18-2013, 02:44 PM   PM User | #3
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
Thanks Fou Lou.


I have got this to work so far.

PHP Code:
$mysqli = new mysqli("localhost""user""pass""db");

$stmt $mysqli->prepare("UPDATE `home` SET `text`='$text' WHERE `home_id`='$home_id'");
$stmt->execute(); 
$stmt->close(); 
Still need to know what else is needed and what is correct..

Thanks. Slayer.
SlayerACC is offline   Reply With Quote
Old 02-18-2013, 03:09 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You need to make sure you connect.
PHP Code:
if ($mysqli->connect_errno)
{
    die(
'Could not connect to database: ' mysqli_connect_error($mysqli)); // this bug is fixed in 5.3+ so you can use $mysqli->connect_error() instead if you have 5.3+

That is also not the proper use of a prepared statement. If you are not making use of prepared statements (and you should be for anything that accepts variable data), than you may as well just use the query method.
PHP Code:
if ($stmt $mysqli->prepare("UPDATE `home` SET `text`=? WHERE `home_id`=?"))
{
    
$stmt->bind_param('ss'$text$home_id);
    
$stmt->execute();
    
$stmt->close();

__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
The Following 2 Users Say Thank You to Fou-Lu For This Useful Post:
salesmachine (02-19-2013), SlayerACC (02-19-2013)
Old 02-18-2013, 09:52 PM   PM User | #5
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
What is an example of the query method?

I am becoming lost again...

What are the differences in how it works?:
PHP Code:
$mysqli = new mysqli("localhost""user""pass""db"); 

$stmt $mysqli->prepare("UPDATE `home` SET `text`='$text' WHERE `home_id`='$home_id'"); 
$stmt->execute();  
$stmt->close(); 
and yours?
PHP Code:
if ($stmt $mysqli->prepare("UPDATE `home` SET `text`=? WHERE `home_id`=?"))
{
    
$stmt->bind_param('ss'$text$home_id);
    
$stmt->execute();
    
$stmt->close();

Thanks... Sorry Fou Lou...


Slayer.
SlayerACC is offline   Reply With Quote
Old 02-18-2013, 10:39 PM   PM User | #6
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,515
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by SlayerACC View Post
What are the differences in how it works?:
PHP Code:
$stmt $mysqli->prepare("UPDATE `home` SET `text`='$text' WHERE `home_id`='$home_id'"); 
and yours?
PHP Code:
if ($stmt $mysqli->prepare("UPDATE `home` SET `text`=? WHERE `home_id`=?"))
{
    
$stmt->bind_param('ss'$text$home_id);

The difference is pretty obvious .. The first one is a bog standard query and you should be using mysqli_query() for it.

The second one is a prepared statement. You see those ? marks? They are called place holders. This tells mysqli that it is a place holder for data which will be supplied seperately. You then bind your data to the appropriate parameters and supply the data seperately. Mysqli then uses that data and does what the statement was telling it to do with it. As I understand this, it deals with the statement and the actual data seperately thus meaning that the query can't be injected with malicious instructions / data because the data is kept seperate.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 02-18-2013, 10:48 PM   PM User | #7
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
Hey Tango..

What is the SS for and where did it come from?

PHP Code:
$stmt->bind_param('ss'$text$home_id); 
SlayerACC is offline   Reply With Quote
Old 02-19-2013, 12:13 AM   PM User | #8
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,515
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
It's the order and type of data.

In this case, you have a string and then another string - hence ss. Then you put your types (in this case the ss) and variables in the same order as the statement - Say you had a string and an integer.. you'd use si and then put your $string and $Integer in bind_param() in that order like this:
bind_param('si', $String, $Integer);

See this page for more:
http://www.php.net/manual/en/mysqli-stmt.bind-param.php
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
SlayerACC (02-19-2013)
Old 02-19-2013, 04:26 AM   PM User | #9
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
Hey thanks again Tango..


This is all seeming to be alot more confusing and complicated to the standard mysql ways...

I will see what I can figure out...Still not sure if I will get this..

I will let ya know..


Slayer.
SlayerACC is offline   Reply With Quote
Old 02-19-2013, 04:44 AM   PM User | #10
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
Okay I think I have this one now. at least I hope so...

I just cant get over all the steps to get it done!!

PHP Code:
$text=$_POST['text'];

$textaddslashes($text);

$stmt = new mysqli("localhost""user""pass""db");

/* check connection */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

$stmt $mysqli->prepare("UPDATE `home` SET `text` = ? WHERE `home_id` = ?");
$stmt->bind_param('si'$text$_POST['home_id']);
$stmt->execute();
$stmt->close(); 
thanks Slayer.
SlayerACC is offline   Reply With Quote
Old 02-19-2013, 06:14 AM   PM User | #11
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
Why does this not work??

PHP Code:
$image=$filename;

$stmt = new mysqli("localhost""user""pass""db");
$stmt $mysqli->prepare("INSERT INTO artist(`artist_id`, `name`, `bio`, `email`, `specialities`, `photo`, `title`) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param('issssss'$_POST['artist_id'], $_POST['name'], $_POST['bio'], $_POST['email'], $_POST['specialities'], $image$_POST['title']);
$stmt->execute();
$newId $stmt->artist_id;
$stmt->close(); 
This is really making me angry... I have tried so many examples to this point .. I should have got this insert by accicdent already..

All help is appreciated.


Thanks, Slayer.
SlayerACC is offline   Reply With Quote
Old 02-19-2013, 06:25 AM   PM User | #12
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
I have it figured out....


Yahoo!!!


PHP Code:
$mysqli = new mysqli("localhost""user""pass""db");
if (
mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}
$stmt $mysqli->prepare("INSERT INTO artist(`artist_id`, `name`, `bio`, `email`, `specialities`, `photo`, `title`) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param('sssssss'$_POST['artist_id'], $_POST['name'], $_POST['bio'], $_POST['email'], $_POST['specialities'], $image$_POST['title']);
$image=$filename;
$stmt->execute();
$newId $stmt->artist_id;
$stmt->close(); 
Slayer
SlayerACC is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:50 PM.


Advertisement
Log in to turn off these ads.