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

时间:2016-11-06 17:04:11

标签: php html

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

te_event,te_venue,te_category。

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

具有venueID,venueName和location

的te_venue表

具有catID和catDesc的te_category。

这是管理员需要选择一个事件才能更新的第一页。

 <!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);
?>

这是管理员可以编辑活动详情的页面

<?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错误:您的SQL语法中有错误;检查手册   对应于您的MariaDB服务器版本,以获得正确的语法   'eventDescription ='附近的PLAYHOUSE惠特利湾很高兴   宣布Litt'在第3行

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

2 个答案:

答案 0 :(得分:2)

您的每个列在查询中都缺少逗号分隔符(,),因此请按以下方式添加: -

$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";
    }

?>

答案 1 :(得分:1)

您这里不使用逗号是查询

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