编辑功能不适用于Php MySql

问题描述 投票:-1回答:3

这是edit.php的代码,当我点击编辑此页面打开并编辑该特定行时。

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<?php
/*
  EDIT.PHP
  Allows user to edit specific entry in database
*/
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($id, $name, $telephone_number, $email,$job_title,$workplace,$country,$nationality, $error){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>Edit Entries</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>';
        }
    ?>
        <div class="maindiv">
            <?php include("includes/head.php");?>
            <?php include("menu.php");?>
            <div class="form_div">
                <div class="title"><h2>Updating Report for ID: <?php echo $id;?></p></h2> </div>
                <form action="" method="post">
                    <link rel="stylesheet" href="css\insert.css" type="text/css" />
                    <link rel="stylesheet" href="css\navcss.css" type="text/css" />
                    <input type="hidden" name="id" value="<?php echo $id; ?>"/>
                    <label>Name:</label><b><label style="margin-left:24em">الاسم</b></label><br />
                    <input class="input" type="text" name="name" value="<?php echo $name; ?>" /><br />
                    <label>Telephone Number:</label><b><label style="margin-left:15em">رقم الهاتف</b><br />
                    <input class="input" type="text" name="telephone_number" value="<?php echo $telephone_number; ?>" /><br />
                    <label>Email:</label></label><b><label style="margin-left:20em">البريد الإلكتروني</b></label>
                    <input class="input" type="text" name="email" value="<?php echo $email; ?>" /><br />
                    <label>Job Title:</label></label><b><label style="margin-left:19em">المسمى الوظيفي</b></label>
                    <input class="input" type="text" name="job_title" value="<?php echo $job_title; ?>" /><br />
                    <label>Work Place:</label></label><b><label style="margin-left:19em">جهه العمل</b></label>
                    <input class="input" type="text" name="workplace" value="<?php echo $workplace; ?>" /><br />
                    <label>Country:</label></label><b><label style="margin-left:23em">الدولة</b></label>
                    <input class="input" type="text" name="country" value="<?php echo $country; ?>" /><br />
                    <label>Nationality:</label></label><b><label style="margin-left:21em">الجنسية</b></label>
                    <input class="input" type="text" name="nationality" value="<?php echo $nationality; ?>" /><br />
                    <p>* Required</p>
                    <input class="submit" type="submit" name="submit" value="Update Record" />
                    <button class="btnSubmit" type="submit" value="Submit" onclick="history.back();return false;">Return to previous page</button>
                 </form>
             </div>
        </div>
    </body>
</html>

<?php } // connect to the database
    include('connect.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['id'])){// get form data, making sure it is valid
            $id = $_POST['id'];
            $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
            $telephone_number = mysql_real_escape_string(htmlspecialchars($_POST['telephone_number']));
            $email = mysql_real_escape_string(htmlspecialchars($_POST['email']));
            $job_title = mysql_real_escape_string(htmlspecialchars($_POST['job_title']));
            $workplace = mysql_real_escape_string(htmlspecialchars($_POST['workplace']));
            $country = mysql_real_escape_string(htmlspecialchars($_POST['country']));
            $nationality = mysql_real_escape_string(htmlspecialchars($_POST['nationality']));// check that firstname/lastname fields are both filled in
            if ($name == ''){// generate error message
                $error = 'ERROR: Please fill in all required fields!';//error, display form
                renderForm($id, $name, $telephone_number, $email, $job_title, $workplace, $country, $nationality, $error);
            }
            else{// save the data to the database
                $link->query("UPDATE conf SET name='$name', telephone_number='$telephone_number',email='$email',job_title='$job_title',workplace='$workplace',country='$country',nationality='$nationality' WHERE id=$id");// 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['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0){// query db
            $id = $_GET['id'];
            $result = $link->query("SELECT * FROM conf WHERE id=$id");
            $row = mysqli_fetch_array($result,MYSQLI_ASSOC);// check that the 'id' matches up with a row in the databse
            if($row){// get data from db
                $name=$row['name'];
                $telephone_number = $row['telephone_number'];
                $email = $row['email'];
                $job_title = $row['job_title'];
                $workplace = $row['workplace'];
                $country = $row['country'];
                $nationality = $row['nationality'];// show form //renderForm($id, $first_name,$emp_number,$department,$email, '');
                renderForm($id, $name, $telephone_number, $email,$job_title,$workplace,$country,$nationality, '');
            }
            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!';
        }
    }
?>    

它首先警告不推荐使用mysql所以我使用了下面的语法,但它仍然给出错误:

mysqli_real_escape_string(htmlspecialchars($link,$_POST['name']));

它给出的第二个主要错误是它将我带到此错误消息并使所有表单字段为空。它显示的线总是:

错误:请填写所有必填字段!

请指导!

php mysql mysqli deprecated
3个回答
0
投票
  $servername = "localhost:3306";
        $username = "root";
        $password = "<Password here>";
        $dbname = "TUTORIALS";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);

        // Check connection
        if ($conn->connect_error) {
           die("Connection failed: " . $conn->connect_error);
        } 
        $sql = "INSERT INTO tutorials_inf(name)VALUES ('".$_POST["name"]."')";

        if (mysqli_query($conn, $sql)) {
           echo "New record created successfully";
        } else {
           echo "Error: " . $sql . "" . mysqli_error($conn);
        }
        $conn->close();
     }

0
投票

我解决了我的自我...

代码如下......

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<?php

/*

EDIT.PHP

Allows user to edit specific entry in database

*/



// creates the edit record form

// since this form is used multiple times in this file, I have made it a function that is easily reusable

function renderForm($id, $name, $telephone_number, $email,$job_title,$workplace,$country,$nationality, $error)

{

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<title>Edit Entries</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>';

}

?>

<div class="maindiv">
<?php include("includes/head.php");?>
<?php include("menu.php");?>
        <!--HTML form -->
            <div class="form_div">
            <div class="title"><h2>Updating Report for ID: <?php echo $id;?></p></h2> </div>
<form action="" method="post">

<link rel="stylesheet" href="css\insert.css" type="text/css" />
<link rel="stylesheet" href="css\navcss.css" type="text/css" />
<input type="hidden" name="id" value="<?php echo $id; ?>"/>

                    <label>Name:</label><b><label style="margin-left:24em">الاسم</b></label>
                    <br />
                    <input class="input" type="text" name="name" value="<?php echo $name; ?>" />
                    <br />
                    <label>Telephone Number:</label><b><label style="margin-left:15em">رقم الهاتف</b>
                    <br />
                    <input class="input" type="text" name="telephone_number" value="<?php echo $telephone_number; ?>" />
                    <br />
                    <label>Email:</label></label><b><label style="margin-left:20em">البريد الإلكتروني</b></label>       
                    <input class="input" type="text" name="email" value="<?php echo $email; ?>" />
                    <br />
                    <label>Job Title:</label></label><b><label style="margin-left:19em">المسمى الوظيفي</b></label>       
                    <input class="input" type="text" name="job_title" value="<?php echo $job_title; ?>" />
                    <br />
                    <label>Work Place:</label></label><b><label style="margin-left:19em">جهه العمل</b></label>       
                    <input class="input" type="text" name="workplace" value="<?php echo $workplace; ?>" />
                    <br />
                    <label>Country:</label></label><b><label style="margin-left:23em">الدولة</b></label>       
                    <input class="input" type="text" name="country" value="<?php echo $country; ?>" />
                    <br />
                    <label>Nationality:</label></label><b><label style="margin-left:21em">الجنسية</b></label>       
                    <input class="input" type="text" name="nationality" value="<?php echo $nationality; ?>" />
                    <br />
<p>* Required</p>
<input class="submit" type="submit" name="submit" value="Update Record" />
<button class="btnSubmit" type="submit" value="Submit" onclick="history.back(); return false;">Return to previous page</button>


</form>
</div>
</div>


</body>

</html>

<?php

}

// connect to the database

$mysqli = new mysqli("sql213.byethost7.com", "b7_21234466", "mazhar2012", "b7_21234466_conference");



// 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['id']))

{

// get form data, making sure it is valid


$id = $_POST['id'];

$name = $mysqli->real_escape_string($_POST['name']);

//$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));

//$last_name = mysql_real_escape_string(htmlspecialchars($_POST['last_name']));

$telephone_number = $mysqli->real_escape_string($_POST['telephone_number']);

$email = $mysqli->real_escape_string($_POST['email']);

$job_title = $mysqli->real_escape_string($_POST['job_title']);

$workplace = $mysqli->real_escape_string($_POST['workplace']);

$country = $mysqli->real_escape_string($_POST['country']);

$nationality = $mysqli->real_escape_string($_POST['nationality']);


// check that firstname/lastname fields are both filled in

if ($name == '')

{

// generate error message

$error = 'ERROR: Please fill in all required fields!';

//error, display form


renderForm($id, $name, $telephone_number, $email, $job_title, $workplace, $country, $nationality, $error);

}

else

{

// save the data to the database

$mysqli->query("UPDATE conf SET name='$name', telephone_number='$telephone_number',email='$email',job_title='$job_title',workplace='$workplace',country='$country',nationality='$nationality' WHERE id=$id");



// 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['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)

{

// query db

$id = $_GET['id'];


$result = $mysqli->query("SELECT * FROM conf WHERE id=$id");



$row = mysqli_fetch_array($result,MYSQLI_ASSOC);



// check that the 'id' matches up with a row in the databse

if($row)

{

// get data from db

        $name=$row['name'];
        $telephone_number = $row['telephone_number'];
        $email = $row['email'];
        $job_title = $row['job_title'];
        $workplace = $row['workplace'];
        $country = $row['country'];
        $nationality = $row['nationality'];


// show form

//renderForm($id, $first_name,$emp_number,$department,$email, '');
renderForm($id, $name, $telephone_number, $email,$job_title,$workplace,$country,$nationality, '');

}

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!';

}

}

?>

0
投票
$link->query($conn,"UPDATE conf SET name='$name', telephone_number='$telephone_number',email='$email',job_title='$job_title',workplace='$workplace',country='$country',nationality='$nationality' WHERE id=$id");
© www.soinside.com 2019 - 2024. All rights reserved.