 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
09-20-2007, 11:02 AM
|
#1 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
What's wrong?
PHP Code:
public function save() { if(isset($this->data['u_id'])) { // UPDATE query on database $this->db->query("UPDATE ".$this->table['members']." (`u_id`, `username`, `password`, `registerdate`, `registerip`) VALUES ('".$this->data['u_id']."', '".$this->data['username']."', '".$this->data['password']."', '".$this->data['registerdate']."', '".$this->data['registerip']."') WHERE `u_id` = '".$this->data['u_id']."'"); }
else { // INSERT query on database $this->db->query('INSERT INTO ' . $this->table['members'] . ' VALUES (NULL, ' . $this->data['username'] . ', ' . $this->data['password'] . ', ' . $this->data['registerdate'] . ', ' . $this->data['registerip'] . ')'); } }
Gives me this:
Code:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(`u_id`, `username`, `password`, `registerdate`, `registerip`) VALUES ('', 'Tana' at line 1
|
|
|
|
09-20-2007, 11:11 AM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
MySQL UPDATE queries are written differently to INSERT queries. An UPDATE query looks like so:
Code:
UPDATE
myTable
SET
MyId = 5,
myUsername = 'Wildhoney'
WHERE
myColumn = 'Foo'
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
09-20-2007, 11:29 AM
|
#3 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
PHP Code:
if(isset($this->data['uid'])) { // UPDATE query on database $this->db->query('UPDATE '.$this->table['members'].' SET username = '.$this->data['username'].', password = '.$this->data['password'].', registerdate = '.$this->data['registerdate'].', registerip = '.$this->data['registerip'].' WHERE uid = '.$this->data['uid']); }
Still gives me:
Quote:
|
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE uid =' at line 2
|
|
|
|
|
09-20-2007, 11:38 AM
|
#4 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
May I first suggest the use of sprintf to improve readability. As for the query, it seems fine except for the lack of quotes around strings. Try this:
PHP Code:
$szSql = sprintf(" UPDATE %s SET username = '%s', password = '%s', registerdate = '%s', registerip = '%s' WHERE uid = %d", $this->table['members'], mysql_escape_string($this->data['username']), mysql_escape_string($this->data['password']), $this->data['registerdate'], $this->data['registerip'], $this->data['uid']);
$this->db->query($szSql);
On another note, how much nicer does that code look? :)
|
|
|
|
09-20-2007, 11:41 AM
|
#5 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Well, the mysql_escape_string is a bit unneccessary, due to this:
PHP Code:
public function setName($name) { $name = $this->db->makesafe($name); $this->data['username'] = (string) $name; }
PHP Code:
public function makesafe($string) { $result = mysql_escape_string($string); return $result; }
But I'll try with the the other things you mentioned! :D
|
|
|
|
09-20-2007, 11:58 AM
|
#6 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Okey, I got it working without any errors now..
PHP Code:
public function save() { if(isset($this->data['uid'])) { // UPDATE query on database $sql = sprintf(" UPDATE %s SET username = '%s', password = '%s', registerdate = '%s', registerip = '%s' WHERE uid = %d", $this->table['members'], $this->data['username'], $this->data['password'], $this->data['registerdate'], $this->data['registerip'], $this->data['uid']); }
else { // INSERT query on database $sql = sprintf(" INSERT INTO % VALUES uid = NULL, username = '%s', password = '%s', registerdate = '%s', registerip = '%s'", $this->table['members'], $this->data['username'], $this->data['password'], $this->data['registerdate'], $this->data['registerip']); } $insert = $this->db->query($sql); if($insert) { return true; } else { return false; } }
PHP Code:
$system->user = new member($system->db, 'users'); $system->user->setName('Tanax'); $system->user->setPass('pass'); $system->user->setRegdate(time()); $system->user->setRegip($_SERVER['REMOTE_ADDR']); if($system->user->save()) { echo '<br />Admin account created!'; } else { echo '<br />' .mysql_error(); }
It printed "Admin account created, however.. when I check the database, no users exists..
|
|
|
|
09-20-2007, 12:20 PM
|
#7 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
OK, the better way to achieve this is to frist execute the INSERT query which will fail on duplicates (assuming of course, you have approprirate PK/unique fields). You then simply determine if the last INSERT actually inserted any data (using mysql_affected_rows), finally, if no data was inserted, you can simply UPDATE the user record. The code should look like this:
PHP Code:
Execute INSERT query
if (mysql_affected_rows() == -1) { Execute UPDATE query }
|
|
|
|
09-20-2007, 12:30 PM
|
#8 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Whoa, too advanced for me karl :|
or what, I use that code that you gave, and just insert the UPDATE query inside the if statement and the INSERT query before?
|
|
|
|
09-20-2007, 12:31 PM
|
#9 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
...Or of course if you wanted to be advanced about this then you could put the SELECT and the UPDATE into one MySQL statement.
Code:
INSERT INTO
myTable
(myKey, myColumn1)
VALUES
('myKey', 'myColumn1')
ON DUPLICATE KEY
UPDATE
myColumn1 = 'myData1'
That's for future reference. * Stops confusing matters *
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
09-20-2007, 12:57 PM
|
#10 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
So in my example, it would look like this?
PHP Code:
$sql = sprintf("INSERT INTO %s (uid, username, password, registerdate, registerip) VALUES ('NULL', '%s', '%s', '%s', '%s') ON DUPLICATE KEY UPDATE username = '%s', password = '%s', registerdate = '%s', registerip = '%s'", $this->table['members'], $this->data['username'], $this->data['password'], $this->data['registerdate'], $this->data['registerip']);
$this->db->query($sql);
|
|
|
|
09-20-2007, 12:45 PM
|
#11 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
Yes you understood that correct, but as Wildhoney has just pointed out, this can be achieved with just one query, which is definately the way to go if your using MySQL 5 or above.
|
|
|
|
09-20-2007, 01:08 PM
|
#12 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
Almost, just a few minor adjustments.
PHP Code:
<?php $sql = sprintf('INSERT INTO %1$s (username, password, registerdate, registerip) VALUES ("%2$s", "%3$s", "%4$s", "%5$s") ON DUPLICATE KEY UPDATE username = "%2$s", password = "%3$s", registerdate = "%4$s", registerip = "%5$s"', $this->table['members'], $this->data['username'], $this->data['password'], $this->data['registerdate'], $this->data['registerip']);
$this->db->query($sql);
?>
If the sprintf placeholders are confusing you in this example, I'd suggest reading Salathe's article which explains how to use them properly, the article can be viewed here.
|
|
|
|
09-20-2007, 01:37 PM
|
#13 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
No no, it's not confusing, it's brilliant :D
Just didn't know of it before
Thanks, it's working :D
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|