利用PHP串連資料庫——實現使用者資料的增刪改查的整體操作執行個體

來源:互聯網
上載者:User

標籤:==   soc   添加   ddc   生日   修改   location   orm   host   

main頁面(首頁面)

<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>代號</td>
<td>姓名</td>
<td>性別</td>
<td>民族</td>
<td>生日</td>
<td>操作</td>
</tr>

<?php
$db = new MySQLi("localhost","root","","mydb");
if(mysqli_connect_error()){

  die("串連失敗");

}
$sql = "select * from Info";
$result = $db->query($sql);
$attr = $result->fetch_all();
foreach($attr as $v)
{
    $sex = $v[2]? ‘男‘:‘女‘;  //三元運算子判斷性別
    $sql = "select Name from Nation where Code =‘$v[3]‘";
    $result = $db ->query($sql);
    $attr = $result->fetch_assoc();
    echo "<tr>
    <td>{$v[0]}</td>
    <td>{$v[1]}</td>
    <td>{$sex}</td>
    <td>{$attr[‘Name‘]}</td>
    <td>{$v[4]}</td>
    <td>
       <a href=‘Delete.php?code={$v[0]}‘>刪除</a>
       <a href=‘Update.php?code={$v[0]}‘>修改</a>
    </td>
    </tr>";
}
?>

</table>
<div>

<a href="Add.php">添加資料</a>

</div>

Add(添加資料頁面)

<h1>添加資料</h1>
<form action="AddChuLi.php" method="post">
    <div>代號:<input type="text" name="code" /></div>
    <div>姓名:<input type="text" name="name" /></div>
    <div>性別:
         <input type="radio" value="男" name="sex" />男
         <input type="radio" value="女" name="sex" />女
    </div>
    <div>民族:
         <select name="nation">
         <?php
         $db = new MySQLi("localhost","root","","mydb");
        if(mysqli_connect_error()){

  die("串連失敗");

  }
         $sql = "select * from Nation";
         $r = $db->query($sql);
         $att = $r->fetch_all();
         foreach($att as $v)
         {
             echo "<option value=‘{$v[0]}‘>{$v[1]}</option>";
         }
         ?>
         </select>
    </div>
    <div>生日:<input type="text" name="birthday" /></div>
    <div><input type="submit" value="添加資料" /></div>
</form>

AddChuLi頁面(添加資料處理頁面)

<?php
$code = $_POST["code"];
$name = $_POST["name"];
$sex  = $_POST["sex"];
$s = 1;
if($sex =="女")
{
    $s=0;
}
$nation = $_POST["nation"];
$birthday = $_POST["birthday"];
$db = new MySQLi("localhost","root","","mydb");
if(mysqli_connect_error()){

  die("串連失敗");

}

$sql = "insert into Info values(‘{$code}‘,‘{$name}‘,‘{$s}‘,‘{$nation}‘,‘{$birthday}‘)";  //添加資料語句
$result = $db->query($sql);
if($result)
{
    header("location:main.php");  //php跳轉頁面方式
}
else
{
    echo "添加失敗!";
}

DeleteChuLi頁面(刪除資料處理頁面)

<?php
$code = $_GET["code"];
$db = new MySQLi("localhost","root","","mydb");
if(mysqli_connect_error()){

  die("串連失敗");

}

$sql = "delete from Info where Code =‘{$code}‘";  //刪除語句
$r = $db->query($sql);
if($r)
{
    header("location:main.php");
}
else
{
    echo "刪除失敗!";
}

Update頁面(修改資料頁面)

<h1>修改資料</h1>
<?php
$code = $_GET["code"];
$db = new MySQLi("localhost","root","","mydb");
if(mysqli_connect_error()){

  die("串連失敗");

}

$sql1 = "select * from Info where Code=‘{$code}‘";
$r1 = $db->query($sql1);
$att1 = $r1->fetch_row();
?>


<form action="UpdateChuLi.php" method="post">
    <div>代號:<input type="hidden" name="code" value="<?php echo $att1[0] ?>" /></div>
    <div>姓名:<input type="text" name="name" value="<?php echo $att1[1] ?>" /></div>
    <div>性別:
         <input type="radio" value="男" name="sex" <?php echo $att1[2] ? "checked=‘checked‘" : ""; ?> />男
         <input type="radio" value="女" name="sex" <?php echo $att1[2] ? "" : "checked=‘checked‘"; ?> />女
    </div>
    <div>民族:
         <select name="nation">
         <?php
         $db = new MySQLi("localhost","root","","mydb");
         if(mysqli_connect_error()){

  die("串連失敗");

  }
         $sql = "select * from Nation";  //查詢nation一組資料
         $r = $db->query($sql);
         $att = $r->fetch_all();
         foreach($att as $v)
         {
             if($att1[3]==$v[0])
             {
                 echo "<option value=‘{$v[0]}‘ selected=‘selectec‘ >{$v[1]}</option>";  
             }
             else
             {
                 echo "<option value=‘{$v[0]}‘>{$v[1]}</option>";
             }
             
         }
         ?>
         </select>
    </div>
    <div>生日:<input type="text" name="birthday" value="<?php echo $att1[4] ?>"/></div>
    <div><input type="submit" value="修改資料" /></div>
</form>

UpdateChuLi頁面(修改資料處理頁面)

<?php
$code = $_POST["code"];
$name = $_POST["name"];
$sex  = $_POST["sex"];
$s=1;
if($sex=="女")
{
    $s=0;
}
$nation = $_POST["nation"];
$birthday = $_POST["birthday"];
$db = new MySQLi("localhost","root","","mydb");
if(mysqli_connect_error()){

  die("串連失敗");

}

$sql = "update Info set Name=‘{$name}‘,Sex={$s},Nation=‘{$nation}‘,Birthday=‘{$birthday}‘ where Code=‘{$code}‘";  //修改資料語句
$r = $db->query($sql);
if($r)
{
    header("location:main.php");
}
else
{
    echo "修改失敗!";
}

利用PHP串連資料庫——實現使用者資料的增刪改查的整體操作執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.