straygrey Posted August 31, 2011 Share Posted August 31, 2011 My php script looks like:- global $UserNum, $UserName, $Password, $JackpotLevel, $Mailshot, $IPAddress, $LastLogon, $ip; $date = date('Y-m-d H:i:s'); $ip = '127.0.0.1'; echo "DingALing = ".$UserNum." ".$UserName." ".$Password." ".$JackpotLevel." ".$Mailshot." ".$IPAddress." ".$LastLogon."<br />"; $SQL = sprintf("INSERT INTO users(Name, Password, JackpotLevel, Mailshot, IPAddress) VALUES ('%s', '%s', '%d', '%d', '%s');", $UserName, $Password, $JackpotLevel, $Mailshot, $ip); $Result = mysql_query($SQL, $link); if (!$Result) die('Invalid query: '.$SQL." ".mysql_error()." ".$Result); I get the following error DingALing = root@asdfghjkl N0B17ch 100000 yes Invalid query: INSERT INTO users(Name, Password, JackpotLevel, Mailshot, IPAddress) VALUES ('root@asdfghjkl', 'N0B17ch', '100000', '0', '127.0.0.1'); but if I now attempt the insert from the command line I get the following success. mysql> describe users; +--------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------+----------------+ | UserNum | int(4) | NO | PRI | NULL | auto_increment | | Name | varchar(35) | YES | | NULL | | | Password | varchar(15) | NO | | NULL | | | JackpotLevel | int(4) | NO | | NULL | | | Mailshot | smallint(2) | YES | | NULL | | | IPAddress | varchar(30) | YES | | NULL | | | lastlogin | datetime | YES | | NULL | | +--------------+-------------+------+-----+---------+----------------+ 7 rows in set (0.00 sec) mysql> select * from users; Empty set (0.00 sec) mysql> INSERT INTO users(Name, Password, JackpotLevel, Mailshot, IPAddress) VALUES ('root@asdfghjkl', 'N0B17ch', '100000', '0', '127.0.0.1'); Query OK, 1 row affected (0.00 sec) mysql> select * from users; +---------+----------------+----------+--------------+----------+-----------+-----------+ | UserNum | Name | Password | JackpotLevel | Mailshot | IPAddress | lastlogin | +---------+----------------+----------+--------------+----------+-----------+-----------+ | 1 | root@asdfghjkl | N0B17ch | 100000 | 0 | 127.0.0.1 | NULL | +---------+----------------+----------+--------------+----------+-----------+-----------+ 1 row in set (0.00 sec) Please tell me what I have done wrong. Link to comment https://forums.phpfreaks.com/topic/246095-insert-failing-from-php-script-but-not-command-line/ Share on other sites More sharing options...
straygrey Posted August 31, 2011 Author Share Posted August 31, 2011 Use $Result = mysql_query($SQL); not $Result = mysql_query($SQL, $link); Link to comment https://forums.phpfreaks.com/topic/246095-insert-failing-from-php-script-but-not-command-line/#findComment-1263859 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.