SQL Doesnt在PHP中运行,但在手动运行时也是如此

时间:2014-06-05 02:59:17

标签: php mysql sql

当我手动将sql查询运行到像Navicat这样的SQL客户端时,一切正常。
当我将它实现到PHP中时,某些东西不起作用。

SQL查询[示例用户名]:

update users
set cardnumber=(select number from cards where used='0' LIMIT 1)
where username='whatever'

我的PHP代码:

<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  header('Location: ../error.php');
  }

$sql =  "update users".
        "set cardnumber=(select number from cards where used='0' LIMIT 1)".
        "where username='".($_SESSION['username'])."'";

mysql_select_db("bluecard");
mysql_query($sql,$con);
mysql_close($con);
header('Location: nextpage.php');
?>

出了什么问题?我找不到了。

[其他工作文件]

<?php
if(! get_magic_quotes_gpc() )
{
   $firstname = addslashes ($_POST['first_name']);
   $lastname = addslashes ($_POST['last_name']);
   $birthday = addslashes ($_POST['datepicker']);
   $sex = addslashes ($_POST['sex']);
   $region = addslashes ($_POST['region']);
}
else
{
   $firstname = $_POST['first_name'];
   $lastname = $_POST['last_name'];
   $birthday = $_POST['datepicker'];
   $sex = $_POST['sex'];
   $region = $_POST['region'];
}

$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  header('Location: ../error.php');
  }


$curruser1  = $_POST['curruser1'];
$sql = "UPDATE users SET firstname='$firstname', lastname='$lastname', birthday='$birthday', sex='$sex', region='$region', order_date = Now() WHERE username='".($_REQUEST['curruser1'])."'";
mysql_SELECT_db("bluecard");
mysql_query($sql,$con);
mysql_close($con);
header('Location: ../-/order/create_account.php');
?>

1 个答案:

答案 0 :(得分:2)

您的SQL正在启动:update usersset cardnumber . . .。你需要一个额外的空间:

$sql =  "update users ".
        "set cardnumber=(select number from cards where used='0' LIMIT 1) ".
        "where username='".($_SESSION['username'])."'";