使用PHP

时间:2016-06-15 08:22:25

标签: php mysql mysqli phpmyadmin

这将是漫长的,所以要做好准备 我制作了这个系统,在其上编辑,删除,添加,显示数据库中的数据。我最初在Mysql中编程了它,但后来想要mysqli中的代码。所以我基本上做的是添加"我"之后我写了mysql。我有一堆页面。 id是bell_no firstname是bell_amount 姓氏是bell_time 期间是bell_period ^我已将这些用作评论中的那些
(database.php中)

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = 'bell_database';

// Create connection
$conn = mysqli_connect($servername, $username, $password, $db);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
?>

(view.php)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>View Records</title>
</head>
<body>

<?php
/* 
    VIEW.PHP
    Displays all data from 'players' table
*/

    // connect to the database
    include('database.php');

    // get results from database
    $result = mysqli_query("SELECT * FROM bell_db") 
        or die(mysqli_error());  

    // display data in table
    echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";

    echo "<table border='1' cellpadding='10'>";
    echo "<tr> <th>Bell No.</th> <th>No. of Rings</th> <th>Time</th> <th>Period</th> <th></th> <th></th></tr>";

    // loop through results of database query, displaying them in the table
    while($row = mysqli_fetch_array( $result )) {

        // echo out the contents of each row into a table
        echo "<tr>";
        echo '<td>' . $row['bell_no'] . '</td>';
        echo '<td>' . $row['bell_amount'] . '</td>';
        echo '<td>' . $row['bell_time'] . '</td>';
        echo '<td>' . $row['bell_period'] . '</td>';
        echo '<td><a href="edit.php?bell_no=' . $row['bell_no'] . '">Edit</a></td>';
        echo '<td><a href="delete.php?bell_no=' . $row['bell_no'] . '">Delete</a></td>';
        echo "</tr>"; 
    } 

    // close table>
    echo "</table>";
?>
<p><a href="new.php">Add a new record</a></p>

</body>
</html> 

new.php

<?php
/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/

 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($bell_amount, $bell_time, $bell_period, $error)
 {
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <title>New Record</title>
 </head>
 <body>
 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 

 <form action="" method="post">
 <div>
 <strong>No. Of Bells: *</strong> <input type="text" name="bell_amount" value="<?php echo $bell_amount; ?>" /><br/>
 <strong>Time: *</strong> <input type="text" name="bell_time" value="<?php echo $bell_time; ?>" /><br/>
 <strong>Period: *</strong> <input type="text" name="bell_period" value="<?php echo $bell_period; ?>" /><br/>
 <p>* required</p>
 <input type="submit" name="submit" value="Submit">
 </div>
 </form> 
 </body>
 </html>
 <?php 
 }




 // connect to the database
 include('database.php');

 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $bell_amount = mysqli_real_escape_string(htmlspecialchars($_POST['bell_amount']));
 $bell_time = mysqli_real_escape_string(htmlspecialchars($_POST['bell_time']));
 $bell_period = mysqli_real_escape_string(htmlspecialchars($_POST['bell_period']));

 // check to make sure both fields are entered
 if ($bell_amount == '' || $bell_time == '' || $bell_period == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($bell_amount, $bell_time, $bell_period, $error);
 }
 else
 {
 // save the data to the database
 mysqli_query("INSERT bell_db SET bell_amount='$bell_amount', bell_time='$bell_time', bell_period='$bell_period'")
 or die(mysql_error()); 

 // once saved, redirect back to the view page
 header("Location: view.php"); 
 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','');
 }
?>

(edit.php)

<?php
/* 
 EDIT.PHP
 Allows user to edit specific entry in database
*/

 // creates the edit  form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($bell_no, $bell_amount, $bell_time, $bell_period, $error)
 {
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
 <title>Edit Record</title>
 </head>
 <body>
 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 

 <form action="" method="post">
 <input type="hidden" name="bell_no" value="<?php echo $bell_no; ?>"/>
 <div>
 <p><strong>Bell No.:</strong> <?php echo $bell_no; ?></p>
 <strong>No Of Rings: *</strong> <input type="text" name="bell_amount" value="<?php echo $bell_amount; ?>"/><br/>
 <strong>Time: *</strong> <input type="text" name="bell_time" value="<?php echo $bell_time; ?>"/><br/>
 <strong>Period: *</strong> <input type="text" name="bell_period" value="<?php echo $bell_period; ?>"/><br/>
 <p>* Required</p>
 <input type="submit" name="submit" value="Submit">
 </div>
 </form> 
 </body>
 </html> 
 <?php
 }



 // connect to the database
 include('database.php');

 // check if the form has been submitted. If it has, process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // confirm that the 'id' value is a valid integer before getting the form data
 if (is_numeric($_POST['bell_no']))
 {
 // get form data, making sure it is valid
 $bell_no = $_POST['bell_no'];
 $bell_amount = mysqli_real_escape_string(htmlspecialchars($_POST['bell_amount']));
 $bell_time = mysqli_real_escape_string(htmlspecialchars($_POST['bell_time']));
 $bell_period = mysqli_real_escape_string(htmlspecialchars($_POST['bell_period']));

 // check that firstname/lastname fields are both filled in
 if ($bell_amount == '' || $bell_time == '' || $bell_period == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 //error, display form
 renderForm($bell_no, $bell_amount, $bell_time, $bell_period, $error);
 }
 else
 {
 // save the data to the database
 mysqli_query("UPDATE bell_db SET bell_amount='$bell_amount', bell_time='$bell_time', bell_period='$bell_period' WHERE bell_no='$bell_no'")
 or die(mysql_error()); 

 // once saved, redirect back to the view page
 header("Location: view.php"); 
 }
 }
 else
 {
 // if the 'id' isn't valid, display an error
 echo 'Error!';
 }
 }
 else
 // if the form hasn't been submitted, get the data from the db and display the form
 {

 // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
 if (isset($_GET['bell_no']) && is_numeric($_GET['bell_no']) && $_GET['bell_no'] > 0)
 {
 // query db
 $bell_no = $_GET['bell_no'];
 $result = mysqli_query("SELECT * FROM bell_db WHERE bell_no=$bell_no")
 or die(mysqli_error()); 
 $row = mysqli_fetch_array($result);

 // check that the 'id' matches up with a row in the databse
 if($row)
 {

 // get data from db
 $bell_amount = $row['bell_amount'];
 $bell_time = $row['bell_time'];
 $bell_period = $row['bell_period'];

 // show form
 renderForm($bell_no, $bell_amount, $bell_time, $bell_period, '');
 }
 else
 // if no match, display result
 {
 echo "No results!";
 }
 }
 else
 // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
 {
 echo 'Error!';
 }
 }
?>

(delete.php)

    <?php
    /* 
     DELETE.PHP
     Deletes a specific entry from the 'players' table
    */

     // connect to the database
     include('database.php');

     // check if the 'id' variable is set in URL, and check that it is valid
     if (isset($_GET['bell_no']) && is_numeric($_GET['bell_no']))
     {
     // get id value
     $bell_no = $_GET['bell_no'];

     // delete the entry
     $result = mysqli_query("DELETE FROM bell_db WHERE bell_no=$bell_no")
     or die(mysqli_error()); 

     // redirect back to the view page
     header("Location: view.php");
     }
     else
     // if id isn't set, or isn't valid, redirect back to view page
     {
     header("Location: view.php");
     }

    ?>
<br>

这些是我建立的所有页面。数据库名为bell_db。它有4列bell_no bell_amount bell_time和bell_period 现在.. database.php工作正常。当我运行view.php时显示以下错误: - 警告:mysqli_query()需要至少2个参数,1在第18行的D:\ xampp \ htdocs \ bell \ view.php中给出

警告:mysqli_error()预计在第19行D:\ xampp \ htdocs \ bell \ view.php中给出1个参数0
当我运行new.php。它显示了表单,但在它上面,它显示了
警告:缺少renderForm()的参数4,在第76行的D:\ xampp \ htdocs \ bell \ new.php中调用,在第9行的D:\ xampp \ htdocs \ bell \ new.php中定义

注意:未定义的变量:第20行的D:\ xampp \ htdocs \ bell \ new.php中的错误 当我填写表格中的东西并按下输入时说明 警告:mysqli_real_escape_string()正好需要2个参数,1在第50行的D:\ xampp \ htdocs \ bell \ new.php中给出

警告:mysqli_real_escape_string()正好需要2个参数,1在第51行的D:\ xampp \ htdocs \ bell \ new.php中给出

警告:mysqli_real_escape_string()正好需要2个参数,第5行给出D:\ xampp \ htdocs \ bell \ new.php 错误:请填写所有必填字段!
如果我打开edit.php它只显示错误!
当我打开delete.php返回view.php

我是这个东西的初学者..所以有人能告诉我出了什么问题吗?

4 个答案:

答案 0 :(得分:1)

对于mysql,您只需连接一次数据库,而对于mysqli,您必须将该连接传递给查询。例如

 $result = mysqli_query($conn, "SELECT * FROM bell_db"); //$conn is your connection variable that you have declared in your code

如上所述,只需将 $ conn 添加到所有mysqli_query

就是这样。它现在会工作...... 您可以查看文档mySqli

答案 1 :(得分:1)

将$ conn添加到mysqli _ *

view.php一样,更改此行

$result = mysqli_query("SELECT * FROM bell_db") 
    or die(mysqli_error()); 

$result = mysqli_query($conn, "SELECT * FROM bell_db") 
    or die(mysqli_error($conn)); 

对其他文件进行更改。

答案 2 :(得分:1)

您需要连接一次

$db = new mysqli($host,$username,$passwd, $dbname);

然后将此对象用于您的查询

$result = $db->query(' SELECT `foo` FROM `bar` WHERE `foofoo`=88 ');

答案 3 :(得分:1)

您必须提供两个参数来传递Mysqli查询中的值。

您必须为数据库的每个条目提供$conn连接查询。

示例:

 $result = mysqli_query($conn, "SELECT * FROM bell_db")