douci1615 2014-02-11 20:00
浏览 44
已采纳

使用主/外键在php中更新数据

Form script:

 <?php
// Inialize session
//session_start();
// Check, if username session is NOT set then this page will jump to login page
/*if (!isset($_SESSION['email'])) {
header('Location: form.html');
}*/
//connecting to the database
define('DB_HOST', 'localhost');
define('DB_NAME', 'online');
define('DB_USER','root');
define('DB_PASSWORD','');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
//inserting Record to the database
$name = $_POST['name'];
$fname = $_POST['fname'];
$cnic =  $_POST['cnic'];
$cellno = $_POST['cellno'];
$nationality = $_POST['nationality'];
$gender =  $_POST['gender'];
$dob = $_POST['dob'];
$religion = $_POST['religion'];
$city=$_POST['city'];
$email = $_POST['email'];
$adress = $_POST['adress'];

 if(!empty ($_POST['name']) && ($_POST['fname']) && ($_POST['cnic']) && ($_POST['cellno']) && ($_POST['nationality']) && (($_POST['gender']==='male')||($_POST['gender']==='female')) && ($_POST['dob']) && ($_POST['religion']) && (($_POST['city']==='Karachi')||($_POST['city']==='Isalamabad')||($_POST['city']==='Lahore')||($_POST['city']==='Rawalpindi'))  && ($_POST['adress']))   //checking the 'user' name which is from Sign-In.html, is it empty or have some text
{
$query = "INSERT INTO basicinfo(name,fname,cnic,cellno,nationality,gender,dob,religion,city,adress)VALUES('$name','$fname','$cnic','$cellno','$nationality','$gender','$dob','$religion','$city','$adress')";
$update = "UPDATE basicinfo SET name='$name',fname='$fname',cnic='$cnic',cellno='$cellno',nationality='$nationality',gender='$gender',dob='$dob',religion='$religion',city='$city',adress='$adress' WHERE bid=";

$result = mysql_query($query);
$updateResult=mysql_query($update);
if($result && updateResult)
    {
        echo "Successfully updated database";
        header('Location: academicinfo.html');
    }
    else
    {
     die('Error: '.mysql_error($con));
    }
    mysql_close($con);
    }
    else{
    header('Location: basicinfo.html');
    }
if (isset ($_POST['neext']))
{
header ('location:academicinfo.html');
}

?>

This is my php code I want to update data but I don't know what to write in where clause because in MySQL database bid column is auto increment and it is foreign key please help me on this.

  • 写回答

1条回答 默认 最新

  • dqfr90979 2014-02-11 20:13
    关注

    Pretty simple just use this for your where condition. This will add another select onto your query but it's not that big of a hit since it's inline. Is that the table you are selecting from?

    
    $update = "UPDATE basicinfo SET name='$name',fname='$fname',cnic='$cnic',cellno='$cellno',nationality='$nationality',gender='$gender',dob='$dob',religion='$religion',city='$city',adress='$adress' WHERE bid= (SELECT bid FROM basicinfo ORDER BY bid ASC LIMIT 1)";
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题