从数据库中获取没有任何名称的数据

时间:2013-11-20 09:27:46

标签: php mysql

<?php
session_start();
require_once("config.php");
      // Insert data into mysql
$sql="INSERT INTO book_seat(`book_id`,`leaving_from`, `going_to`, `datepick`, `    total_amt`, `total_seats`, `boarding_point`) VALUES ('',
            '{$_SESSION['data'][source_point]}',
            '{$_SESSION[destination]}',
            '{$_SESSION[datepick]}',
            '{$_SESSION[total_amt]}',
            '{$_SESSION[total_seats]}',
            '{$_SESSION[boarding_point]}')";
$result=mysql_query($sql);
if (!$result) { die(mysql_error()); }

if(isset($_POST['chksbmt']) && !$errors) { header("location:booking_detail.php"); }

mysql_close();

?>
  

这里我的值被插入到数据库中,但我想根据book_id获取ans显示数据。   但即使借助会话变量我也无法获取数据,我只想根据主键搜索和显示,即book_id

3 个答案:

答案 0 :(得分:1)

首先更改您的插入查询... bcz在您的查询中,您尝试插入7个字段,并且您传递的是6个值...( book_id应该在这种情况下自动递增)< / p>

   $sql="INSERT INTO book_seat(`leaving_from`, `going_to`, `datepick`, `total_amt`, `total_seats`, `boarding_point`) VALUES ('',
        '{$_SESSION['data'][source_point]}',
        '{$_SESSION[destination]}',
        '{$_SESSION[datepick]}',
        '{$_SESSION[total_amt]}',
        '{$_SESSION[total_seats]}',
        '{$_SESSION[boarding_point]}')";

  $result=mysql_query($sql);
  if (!$result) { die(mysql_error()); }

  $last_insert_book_id= mysql_insert_id() //here you will get last inserted book_id

  $result=mysql_query("select * from book_sear where book_id=".$last_insert_book_id."");

  $row=mysql_fetch_array($result);
  echo $row['leaving_from']; //will print last inserted leaving from

注意

  • 不要使用Mysql _ ** bcz它已被弃用。使用mysqli _ **或PDO
  • 不要以这种方式插入...尝试使用prepare语句或转义字符串

希望它可以帮到你

答案 1 :(得分:0)

您应该尝试mysql_insert_id() printf("Last inserted record has id %d\n", mysql_insert_id());,现在可以获取数据

//示例

$last_id = mysql_insert_id();
$result = mysql_query('select * from book_seat where book_id ='. $last_id);

对于PHP5

您可以使用mysqli创建连接,插入和获取最后一个ID,例如

$mysqli = new mysqli("localhost", "my_user", "my_password", "databasenam");
$query = "INSERT INTO Table VALUES (NULL, 'value1')";
$mysqli->query($query);
printf ("New Record has id %d.\n", $mysqli->insert_id);

答案 2 :(得分:0)

如果您想要当前表中的最新ID,则必须在该表上执行SELECT MAX(book_id),其中id是ID column的名称。
LAST_INSERT_ID()只能告诉您ID最近自动生成的ID的entire database connection

(建议PDO然后mysql_query

Select * from book_seat where book_id in (Select MAX(book_id) from book_seat)

按ID搜索(而不是按最后或最晚)

Select * from book_seat where book_id = YOUR ID VARIABLE