简体   繁体   中英

Update function does not work properly

In my php code is I have faces two problem. Code is below:

file name: editevent.php

<?php 

    $server_name    = 'localhost';
    $username       ='root';
    $password       ='';
    $db_name        ='reminder';

    $connect_error  = 'Sorry, we\'re experiencing connection problems.';
    $con = mysql_connect($server_name, $username, $password);
    if(!$con){
        die($connect_error);
    } 
    mysql_select_db($db_name) or die($connect_error);
 ?>

<?php

    if (isset($_POST['update_event'])) {
        $update_query = "UPDATE calendar_event SET send_date = '$_POST[date]', send_time = '$_POST[time]', event_name = '$_POST[event_name]', message = '$_POST[message]', email = '$_POST[email]', phone_no = '$_POST[phone_no]' WHERE id= '$_POST[id]'";
        mysql_query($update_query, $con);
     } 

    $sql = "SELECT * FROM calendar_event";
    $mydata = mysql_query($sql, $con);
    while($record = mysql_fetch_array($mydata)){
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Add Event</title>
    <link rel="stylesheet" href="css/index.css">
    <link rel="stylesheet" href="css/addevent.css">
    <link rel="stylesheet" href="css/editevent.css">
</head>
<body>
    <form action="editevent.php" method="post">
        <ul>
            <div class="time_date">
                <li class="date">
                    Date: <br>
                    <input type="date" name="date" value="<?php echo $record['send_date']; ?>"/>
                </li>
                <li class="time">
                    Time: <br>
                    <input type="time" name="time" value="<?php echo $record['send_time']; ?>"/>
                </li>
            </div>

            <li>
                Event Name: <br>
                <input type="text" name="event_name" style="width: 285px;" value="<?php echo $record['event_name']; ?>"/>
            </li>
            <li>
                Your Message: <br>
                <textarea name="message" style="width: 285px; height: 100px;"><?php echo $record['message']; ?></textarea>
            </li>
            <div class="email_phoneno">
                <li class="email">
                    Email: <br>
                    <input type="email" name="email" style="width: 150px;" placeholder="example@email.com" maxlength="30"  value="<?php echo $record['email']; ?>">
                </li>
                <li class="phoneno">
                    Phone No: <br>
                    <input type="tel" style="width: 100px;" name="phone_no" value="<?php echo $record['phone_no']; ?>"/>
                </li>
            </div>
            <div class="update_delete_button">
                <li class="save_button">
                    <input type="submit" value="Save event" name="update_event">
                </li>
            </div>

        </ul>
    </form>
                    <li><a href="index.php">home</a></li>

</body>
</html>
<?php
break 1;
    }
    mysql_close($con);
 ?>

When I run this code it shows

***Notice: Undefined index: `id` in `C:\xampp\htdocs\window\editevent.php`
on line 19***

When I avoid WHERE clause full database table has been updated. How can I solve it?

Note: id is an auto-increment column in my table.

在表单 TAG 中添加此代码

<input type="id" type="hidden" style="width: 100px;" name="id" value="<?php echo $record['id']; ?>"/>

Before you extract values from $_POST, you should check if they exist. You could use the isset function for this function.isset

Once check $_POST is properly set OR not.

Use below mysql query string for your solution.

$update_query = "UPDATE calendar_event SET send_date = '" . $_POST['date'] . "', send_time = '" . $_POST['time'] . "', event_name = '" . $_POST['event_name'] . "', message ='" . $_POST['message'] . "', email = '" . $_POST['email'] . "', phone_no = '" . $_POST['phone_no'] . "' WHERE id= '" . $_POST['id'] . "'";

Let me now if there is any concern regarding this.

You see this notice because the index "id" does not exist in $_POST. As a thumb rule, always remember:

  1. Always check if that variable exist in $_REQUEST (POST/GET) ie array_key_exists function is used to check.

    if(array_key_exists("id",$_POST){ // code here}

  2. Always check if that variable has a value . isset function is used for this purpose.

    if(isset($_POST['id']){ // code here}

In your scenario, your update form does not have reference "id" which you are using in update query.

You have to add following html before form close:

<input type="id" type="hidden" style="width: 100px;" name="id" value="<?php echo $record['id']; ?>"/>

Note: It's always a good practice to turn on all error and notices in development environment.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM