dongxinche1264 2013-03-29 17:06
浏览 32
已采纳

将表单数据输入到MySql表中,然后在PHP中直接搜索该表[关闭]

After submitting a form and inserting the data into a table, I want to search this table and set a session variable to use on another database insert. Below is the PHP which inserts and then searches the newly updated table.

require_once("db_connect.php");

if($db_server) {

     mysql_select_db($db_database) or die ("<p>Couldn't find database.</p>");
     $query = "INSERT INTO routes (user_id, title, description, startlat, startlng, endlat, endlng) VALUES ('$user_id', '$title', '$desc', '$startlat.', '$startlng', '$endlat', '$endlng')";
     if (mysql_query($query)) {
          $get_id = mysql_query("SELECT * FROM routes WHERE user_id, title = '$user_id', '$title'");
          $route_id = mysql_result($get_id, 0,'id');
          $_SESSION['route_id'] = $route_id;
          echo $route_id; 
     } else {
          $message = ("Insert failed. " . mysql_error() . "<br/>" . $query);
     }  
} else {
     $message = "Error: could not connect to the database.";
}
mysql_close($db_server);

I get this warning:

 Warning: mysql_result() expects parameter 1 to be resource, boolean given in... 

which refers to this line of the above code:

 $route_id = mysql_result($get_id, 0,'id');

Basically what I am attempting to do is get the 'id' of the newly inserted row (the id is the primary key and is auto incremented) and set it as the $route_id so that on my next page I can insert this value into another table as a route_id which is used to link the two tables together.

  • 写回答

1条回答 默认 最新

  • douqiangchuai7674 2013-03-29 17:07
    关注
    SELECT * FROM routes WHERE user_id, title = '$user_id', '$title'
    

    The one above is an invalid query. That is why your function fails and doesn't return the expected resource. Its Proper syntax is

    SELECT * FROM routes WHERE user_id= '$user_id' and title= '$title'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么