更新不同表中的数据SQL数据库


update data in different table sql database

首先,我的数据库中有3个表:-

te_event, te_venue, te_category.

te_event表显示categoryID、venueID、事件描述、标题、日期和价格。

te_venue表有venueID, venueName和location

包含catID和catDesc的te_category。


这是admin需要选择一个事件以便更新的第一个页面。
 <!DOCTYPE html>
<html lang="en">
<html>
<head>
    <link rel="stylesheet" type="text/css" href="test1.css">
    <meta charset = "utf-8">
<title>
</title>
</head>
<body>
<div id="title">
         <p><h1>Tyne Events</h1></p>
    </div>

<div id="wrapper">
    <div id="navbar" > 
            <ul class="nav">
                 <li><a href="home.html">Home</a></li>
                 <li><a href="findoutmore.php">Find out more</a></li>
                 <li><a href="offer.html">Offer</a></li>
                 <li><a href="credit.html">Credit</a></li>
                 <li><a href="restrictedPage.php">Admin</a></li>
                 <li> 
                    <form class="formright">
                    <input  type="text" placeholder="Search">
                    <button  type="submit">Search</button>                                          
                    </form> 
                </li>
            </ul>

        </div>  
    <div id= "detailCenter">
        <?php
    include "database_conn.php";
    $sqlEvent = "SELECT * FROM te_events
                 INNER JOIN te_venue ON te_events.venueID = te_venue.venueID
                 INNER JOIN te_category ON te_events.catID = te_category.catID
                 ORDER by eventTitle    ";//select all event record

    //query the statement
    $event= mysqli_query ($conn , $sqlEvent)
    or die (mysqli_error($conn));
?>

<h1>Select Event To Modify</h1>
<table border = "1" cellpadding="10" width=100% >
<thead>
    <th>Title</th>
    <th>Category</th> 
    <th>Venue Name</th>
    <th>Location</th>
    <th>Start Date</th>
    <th>End Date</th>
    <th>Price</th>
</thead>
<?php
    //display all the event record
    while ($row = mysqli_fetch_assoc ($event)){
        //extract the field
        $id         = $row  ["eventID"];
        $title      = $row  ["eventTitle"];
        $desc       = $row  ["eventDescription"];
        $venue      = $row  ["venueName"];
        $location   = $row  ["location"];
        $category   = $row  ["catDesc"];
        $eStart     = $row  ["eventStartDate"];
        $eEnd       = $row  ["eventEndDate"];
        $ePrice     = $row  ["eventPrice"];

        //start a row
        echo"<tr>'n";
        //output the URL
        echo "<td>'n";
        echo "<div> <a href = '"allDetails.php?eventID=$id'">
                    $title</a></div>'n";
        echo "</td>'n";
        echo "<td>'n";
        echo "<div> $category</div>'n";
        echo "</td>'n";
        echo "<td>'n";
        echo "<div> $venue</div>'n";
        echo "</td>'n";
        echo "<td>'n";
        echo "<div> $location</div>'n";
        echo "</td>'n";
        echo "<td>'n";
        echo "<div>$eStart</div> 'n";
        echo "</td>'n";
        echo "<td>'n";
        echo "<div>$eEnd</div>'n";
        echo "</td>'n";
        echo "<td> 'n";
        echo "<div>$ePrice</div>'n";
        echo "</td>'n";
    }
?>
</table>
</div>
</body>
</html>
<?php
    mysqli_close($conn);
?>

这是admin可以编辑事件详细信息的页面

<?php
    include "database_conn.php";
    if(isset($_GET['eventID'])){
        $id = $_GET["eventID"]; //get event id
    }
    else {
        header ("Refresh : 3; url=admin.php");//redirect to choose title
        die ("Please use the Choose Event Title List");
    }
    //get event id
    $id = $_GET["eventID"];//get event id
    //select event
    $sqlEvent =" SELECT * FROM te_events 
                 INNER JOIN te_venue ON te_events.venueID = te_venue.venueID 
                 INNER JOIN te_category ON te_events.catID = te_category.catID
                 WHERE te_events.eventID =".$id;

    //excute sql statemente
    $event = mysqli_query($conn , $sqlEvent) or die (mysqli_error($conn));
    $_GET = mysqli_fetch_assoc($event) or die (mysqli_error($conn));
    //extract each field$
        $title      = $_GET ["eventTitle"];
        $desc       = $_GET ["eventDescription"];
        $venueId    = $_GET ["venueID"];
        $venue      = $_GET ["venueName"];
        $location   = $_GET ["location"];
        $categoryId = $_GET ["catID"];
        $category   = $_GET ["catDesc"];
        $eStart     = $_GET ["eventStartDate"];
        $eEnd       = $_GET ["eventEndDate"];
        $ePrice     = $_GET ["eventPrice"]; 
?>
<!DOCTYPE html>
<html lang="en">
<html>
<head>
    <link rel="stylesheet" type="text/css" href="test1.css">
    <meta charset = "utf-8">
<title>
</title>
</head>
<body>
<div id="title">
         <p><h1>Tyne Events</h1></p>
    </div>

<div id="wrapper">
    <div id="navbar" > 
            <ul class="nav">
                 <li><a href="home.html">Home</a></li>
                 <li><a href="findoutmore.php">Find out more</a></li>
                 <li><a href="offer.html">Offer</a></li>
                 <li><a href="credit.html">Credit</a></li>
                 <li><a href="restrictedPage.php">Admin</a></li>
                 <li> 
                    <form class="formright">
                    <input  type="text" placeholder="Search">
                    <button  type="submit">Search</button>                                          
                    </form> 
                </li>
            </ul>

        </div>  
    <div id= "detailCenter">
<form id="updateEventDetail" method ="get" action ="updateEventDetail.php">
<fieldset>
    <legend>Event details</legend>

            <div><p>
            <input type ="hidden" name ="eventID" value="<?=$eventID?>"/>
            </p></div>
            <div><p>
            <label class="field" for="eTitle">Event title</label>
            <input type ="text" name ="eventTitle" value="<?=$title?>"/>
            </p></div>
            <div><p>
            <label class="field" for="cat">Category</label>
            <?php
            $sqlCategory ="SELECT DISTINCT catDesc FROM te_category ORDER BY 1";
            //query sqlVenue
            $rsCategory = mysqli_query ($conn ,$sqlCategory)
                        or die ("SQL ERROR :".mysqli_error($conn));
            //create select item
            echo"<select name='"catDesc'">'n";
            //iterate venue record
            while ($_GET = mysqli_fetch_assoc($rsCategory)){
                //populate select item
                $category = $_GET[catDesc]; //get each venueName record
                if($category==$category)
                    echo "<option value ='"$category'" selected>
                        $category</option>'n";
                else{
                    echo "<option value ='"$venue'">
                        $category</option>'n";
                }
            }
            echo"</select>'n";
            ?>
            </p></div>

            <div><p>
            <label class="field" for="desc">Description</label>
            <textarea style="resize:none" name="eventDescription" rows="10" cols="40" value="<?=$desc?>"><?=$desc?> </textarea>
            </p></div>
            <div>
            <p>
            <label class ="field" for="venue">Venue</label>
            <?php
            $sqlVenue ="SELECT DISTINCT venueName FROM te_venue ORDER BY 1";
            //query sqlVenue
            $rsVenue = mysqli_query ($conn ,$sqlVenue)
                        or die ("SQL ERROR :".mysqli_error($conn));
            //create select item
            echo"<select name='"venueName'">'n";
            //iterate venue record
            while ($_GET = mysqli_fetch_assoc($rsVenue)){
                //populate select item
                $eVenue = $_GET[venueName]; //get each venueName record
                if($eVenue==$venueName)
                    echo "<option value ='"$eVenue'" selected>
                        $eVenue</option>'n";
                else{
                    echo "<option value ='"$venue'">
                        $eVenue</option>'n";
                }
            }
            echo"</select>'n";
            ?>
            </p>
            </div>
            <div><p>
            <label class="field" for="location">Location</label>
            <?php
            $sqlLocation ="SELECT DISTINCT location FROM te_venue ORDER BY 1";
            //query sqlLocation
            $rsLocation = mysqli_query ($conn ,$sqlLocation)
                        or die ("SQL ERROR :".mysqli_error($conn));
            //create select item
            echo"<select name='"location'">'n";
            //iterate venue record
            while ($_GET = mysqli_fetch_assoc($rsLocation)){
                //populate select item
                $eLocation = $_GET[location]; //get each location record
                if($eLocation==$location)
                    echo "<option value ='"$eLocation'" selected>
                        $eLocation</option>'n";
                else{
                    echo "<option value ='"$location'">
                        $eLocation</option>'n";
                }
            }
            echo"</select>'n";
            ?>
            </p></div>
            <div><p>
            <label class="field" for="sDate">Event Start Date</label>
            <input name="eventStartDate" type="date"  value="<?=$eStart?>"/>
            </p></div>
            <div><p>
            <label class="field" for="eDate">Event End Date</label>
            <input name="eventEndDate" type="date"  value="<?=$eEnd?>"/>
            </p></div>
            <div><p>
            <label class="field" for="ePrice">Event Price</label>
            <input type="text" name="eventPrice" value="<?=$ePrice?>"readonly/>
            </p></div>
            <div><p>
            <input type="submit" id="floatright" value="Update Event"/>
            </p></div>
</fieldset>
</form>
</div>
</body>
</html>
<?php 
    mysqli_close($conn);
?>

下面的代码显示updatedetail.php

<?php   
    include "database_conn.php";
    if(isset($_GET['eventID'])){
        $id = $_GET["eventID"]; //get event id
    }
    //get all data submited
        $id          = $_GET    ["eventID"];
        $title       = $_GET    ["eventTitle"];
        $desc        = $_GET    ["eventDescription"];
        $venue       = $_GET ["venueName"];
        $location    = $_GET ["location"];
        $category    = $_GET ["catDesc"];
        $eStart      = $_GET ["eventStartDate"];
        $eEnd        = $_GET ["eventEndDate"];
        $ePrice      = $_GET["eventPrice"];
        $sqlUpdateEvent             = " UPDATE te_event SET
                 eventTitle         = '$title'
                 eventDescription   = '$desc'
                 catDesc            = '$category'
                 venueName          = '$venue'
                 location           = '$location'
                 eventStartDate     = '$eStart'
                 eventEndDate       = '$eEnd'
                 eventPrice         = '$ePrice'
                 WHERE eventID      =  $id";
        mysqli_query ($conn , $sqlUpdateEvent)
            or die ("SQL ERROR :".mysqli_error($conn));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>update mvie comfirmmation</title>
<meta charset="utf-8">
</head>
<body>
<h1>update mvoie detials</h1>
<?php 
    echo "Title : $eventTitle 'n";
    echo "Category : $catDesc 'n";
    echo "Description : $eventDescription 'n";
    echo "Venue : $venueName 'n";
    echo "Location : $location 'n";
    echo "Start Date : $eventStartDate 'n";
    echo "End Date : $eventEndDate 'n";
    echo "Price : $eventPrice 'n";
    if(mysqli_affected_rows($conn)>0)
        echo "<p> Event update successfully </p>'n";
    else
        echo "<p> Update Fail</p>'n";
?>
<a href = "admin.php">Choose Event</a>
</body>
</html>
<?php
    mysqli_close($conn);
?>
我得到的错误是

SQL ERROR:你的SQL语法有错误;查看手册对应于您的MariaDB服务器版本以使用正确的语法惠特利湾PLAYHOUSE很高兴宣布Litt'在第3行

我通过添加一些代码编辑了我的问题
我希望它能有所帮助
谢谢你的帮助。

您的查询中每个列都缺少逗号分隔符(,),因此添加它们如下:-

$sqlUpdateEvent = "UPDATE te_event SET eventTitle = '$title', eventDescription = '$desc',catDesc = '$category',venueName = '$venue',location = '$location',eventStartDate = '$eStart',eventEndDate = '$eEnd',eventPrice = '$ePrice' WHERE eventID =  $id";

建议:-你的查询是开放给SQL Injection,所以尝试阅读prepared statements和使用它们。

代码增强:-

<?php   
    include "database_conn.php";
    if(isset($_GET['eventID'])  && isset($_GET["eventTitle"]) && isset($_GET["eventDescription"]) && isset($_GET["venueName"]) && isset($_GET["location"]) && isset($_GET["catDesc"]) && isset($_GET["eventStartDate"]) && isset($_GET["eventEndDate"]) && isset($_GET["eventPrice"]) ){
        $id = $_GET["eventID"];
        $title       = $_GET["eventTitle"];
        $desc        = $_GET["eventDescription"];
        $venue       = $_GET["venueName"];
        $location    = $_GET["location"];
        $category    = $_GET["catDesc"];
        $eStart      = $_GET["eventStartDate"];
        $eEnd        = $_GET["eventEndDate"];
        $ePrice      = $_GET["eventPrice"];
        $sqlUpdateEvent = " UPDATE te_event SET eventTitle = '$title',eventDescription = '$desc',catDesc = '$category',venueName = '$venue',location = '$location',eventStartDate = '$eStart',eventEndDate = '$eEnd',eventPrice = '$ePrice' WHERE eventID =  $id";
        mysqli_query ($conn , $sqlUpdateEvent) or die ("SQL ERROR :".mysqli_error($conn));
    }else{
       echo "all data is required";
    }
?>

您没有使用逗号,这里是查询

$sqlUpdateEvent  = " UPDATE te_event SET
                 eventTitle         = '$title',
                 eventDescription   = '$desc',
                 catDesc            = '$category',
                 venueName          = '$venue',
                 location           = '$location',
                 eventStartDate     = '$eStart',
                 eventEndDate       = '$eEnd',
                 eventPrice         = '$ePrice'
                 WHERE eventID      =  $id";