插入mysql / php困难

时间:2012-10-04 04:27:39

标签: php mysql

我正在尝试制作一个拍卖网站并将其设为所以我可以在mysql表中更新它,但它不会更新mysql表中的东西。我得到一个php错误日志,我得到的所有错误来自

t-2012 17:02:46] PHP Notice:  Undefined variable: auctionnum in /devel/cgreenheld/projects/Asgn2/auction.php on line 21

[04-Oct-2012 17:02:46] PHP Notice:  Undefined variable: reserve_met in /devel/cgreenheld/projects/Asgn2/auction.php on line 116

[04-Oct-2012 17:02:46] PHP Notice:  Undefined variable: reserve in /devel/cgreenheld/projects/Asgn2/auction.php on line 123

[04-Oct-2012 17:02:46] PHP Notice:  Undefined variable: highest_bid in /devel/cgreenheld/projects/Asgn2/auction.php on line 123

[04-Oct-2012 17:02:46] PHP Notice:  Undefined variable: highest_bid in /devel/cgreenheld/projects/Asgn2/auction.php on line 123
[04-Oct-2012 17:02:46] PHP Notice:  Undefined variable: auctionnum in /devel/cgreenheld/projects/Asgn2/auction.php on line 124

[04-Oct-2012 17:02:46] PHP Notice:  Undefined variable: title in /devel/cgreenheld/projects/Asgn2/auction.php on line 130

[04-Oct-2012 17:02:46] PHP Notice:  Undefined variable: bio in /devel/cgreenheld/projects/Asgn2/auction.php on line 131

[04-Oct-2012 17:02:46] PHP Notice:  Undefined variable: reserve_met in /devel/cgreenheld/projects/Asgn2/auction.php on line 133

[04-Oct-2012 17:02:46] PHP Notice:  Undefined variable: reserve in /devel/cgreenheld/projects/Asgn2/auction.php on line 134

我有相当多的代码,所以我会尝试将其缩小到我认为相关的位。这都是非常基本的atm,但我只想让它能够将内容插入到mysql表中。 所以这是他们输入以插入拍卖的形式(这很好)

<h2>Create a new Auction!</h2>
    <?php
        echo "<form method='post' action='createauction.php' enctype=multipart/form-data>";
    echo '<fieldset class="register">';
    echo"<h2>Auction Properties</h2>";
        echo "<ul>";
                echo '<li><label for="Title">Auction Title:</label> <input type="text" name="Title" id="Title"></li>';
                echo '<li><label for="Bio">Auction Description </label> <input type="text" name="Bio" id="Bio"></li>';
                echo '<li><label for="Reserve">Reserve</label><input type="text" name="Reserve" id="Reserve"></li>';
                echo '<li id="contentbox">Category:</li>';
                echo '<li><select name="Category"></li>';
                echo '<li><option value="clothing">Clothing</option></li>';
                echo '<li><option value="electronics">Electronics</option></li>';
                echo '<li><option value="living">Living</option></li>';
                echo '<li><input type="submit" value="Create"></li>';
                echo "</ul>";
    echo "</fieldset>";
    echo "</form>";
    ?>

这是将它插入表中(这也可以正常工作)

<?php
    $clean_title = $_POST['Title'];
    $clean_bio = $_POST['Bio'];
    $clean_category = $_POST['Category'];
    $username = $_SESSION['user'];
    $reserve = $_POST['Reserve'];
    $insert = 'Insert INTO Auctions(closingtime, Title, Bio, Category, Username, reserver) VALUES (NOW() + INTERVAL 7 DAY,  "'.$clean_title.'", "'.$clean_bio.'", "'.$clean_category.'", "'.$_SESSION['user'].'", "'.$reserve.'")'; 
    $result = $conn->query($insert);
    $auction_query = "select Auctionno from Auctions ORDER BY Auctionno";
    $result2 = $conn->query($auction_query);
        while ($row = $result2->fetch_assoc()) {
        $auctionnum = $row['Auctionno'];
        }
?>

<p>Your auction has been started click <a href="auction.php?id=<?php echo $auctionnum;?>">here</a> to view it.
            </p>

它将我们带到查看我们拍卖的页面 然后我们进入我们的拍卖页面,

if(isset($_GET['id'])){
  $auctionnum = $_GET['id'];
 }else{
  header("Location: browse.php");
  exit();
 }

$query = 'SELECT * FROM Auctions WHERE Auctionno = "'.$auctionnum.'";';
$result = $conn->query($query);
    while ($row = $result->fetch_assoc()) {
    $auctionnum = $row['Auctionno'];
    $title = $row['Title'];
    $bio = $row['Bio'];
    $category = $row['Category'];
    $reserve = $row['reserver'];
    $reserve_met = $row['reserve_MET'];
    $highest_bid = $row['highest_bid'];
    }
?>
<!DOCTYPE html>

<html>
    <head>
            <!--[if IE]>
    <link rel="stylesheet" href="IEstyle.css"/>
    <script>
    document.createElement('nav');
    document.createElement('section');
    document.createElement('footer'):
    document.createElement('header');
    </script>
    <![endif]-->
        <title>Bargin Bin: A place where Kiwis can buy and sell!</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="auction.css">
    </head>
    <body>
    <header>
        <img src="logo.gif" alt="Bargin Bin Logo">
        <ul>
        <li><a href="login.php"><b>Login</b></a></li>
        <li><a href="register.php"><b>Register</b></a></li>
        </ul>
        <?php
            if($_SESSION['authorisation'] == 'knownuser') {
                if(isset($_SESSION['user'])) {
        echo '<ul class="log">';
        echo '<li><h4>You are logged in as '.$_SESSION['user'].'</h4></li>';
        echo '<li><a href="logout.php">Logout</a></li>';
        echo "</ul>";
            }
        }
        else {
        echo "<h4>You are not logged in</h4>";
        }
        ?>
    </header>
    <nav>
    <ul>
    <li><a href="index.php">Home</a></li>
    <li><a href="browse.php"><b>Browse</b></a></li>
    <?php
    if(isset($_SESSION['authorisation'])) {
    if($_SESSION['authorisation'] == 'knownuser') {
    echo '<li><a href="account.php">MyAccount</a></li>';
    }
    }
    if(isset($_SESSION['admin'])) {
    echo '<li><a href="admin.php">Admin</a></li>';
    }
    if(isset($_SESSION['authorisation'])) {
    if($_SESSION['authorisation'] = 'knownuser') {
    echo '<li><b>MyAuction</b></li>';
    }
    }
    ?>
    </ul>
    </nav>
    <section>   
    <div id="bidbar">
    <?php
echo "<form action=\"bid.php\" method=\"post\">";
echo "<div id=\"bidbar\">";
if($reserve_met == 0){
  echo "<span>Starting bid:$</span>";
}else{
  echo "<span>Min next bid: $</span>";
}
?>

<input id="bidinput" type="text" name="bid" value="<?php echo $reserve > $highest_bid ? $reserve : $highest_bid + 1;?>" size="4">
<input type="hidden" name="auction_number" value="<?php echo $auctionnum;?>">
<input type="submit" id="bidbutton" value="click here to bid">
</div><!-- //bidbar -->
</form>

<?php
    echo "<h1 id=\"auctiontitle\">Auction Title: $title </h1>";
    echo "<p id=\auctionbio\">Auction Description: $bio</p>";
    echo "<div id=\"infobar\">";
    if($reserve_met == 0){
        echo "<span>Start Price: \$$reserve</span>";
        echo "</br>";
         echo "<span id=\"reserve\">No reserve</span>";
        }else{
        echo "<span>Highest Bid:\$$highest_bid</span>";
        echo "<span id=\"reserve\">Reserve Met</span>";
    }

?>
    </section>
    <hr>
    <footer>Courtney Greenheld Web Design 2012 Ltd.</footer>
    </body>
    </html>

然后这是我处理出价的页面。

if(isset($_POST['bid']) && isset($_POST['auctionnum'])) {
  $clean_bid = $_POST['bid'];
echo $clean_bid;
  $auctionnum = $_POST['auctionnum'];
echo $auctionnum;
  $bidder = $_SESSION['user'];
echo $bidder;
}else{
  header("Location: auction.php");
  exit();
}
$query_highest_bid = 'SELECT highest_bid FROM Auctions WHERE Auctionno = "'.$auctionnum.'";';
$result = $conn->query($insert);
$highest_array = mysql_fetch_array($result);
$highest_bid = $highest_array[0];
$query1 = 'UPDATE Auctions SET highest_bid = "'.$clean_bid.'" WHERE Auctionno = "'.$auctionnum.'";';
$result1 = $conn->query($query1);
$query2 = 'UPDATE Auctions SET highest_bidder = "'.$bidder.'" WHERE Auctionno = "'.$auctionnum.'";';
$result2 = $conn->query($query2);
$query3 = 'UPDATE Auctions SET highest_bidder = "'.$bidder.'" WHERE Auctionno = "'.$auctionnum.'";';
$result3 = $conn->query($query3);
$query4 = 'INSERT INTO BiddingUsers(Auctionno, Username, Bid) VALUES("'.$auctionnum.'" , "'.$bidder.'", "'.$clean_bid.'");';
$result4 = $conn->query($query4);
header("Location: auction.php?id=$auctionnum");

?>

我不确定它到底出了什么问题,我认为mysql_fetch_array是一个问题,我通常不会使用它...但我很难过,哪些是错的。 而且我还没有对我的输入进行过分析,但我知道它们。 谢谢。

3 个答案:

答案 0 :(得分:1)

数据库的连接在哪里? ($ conn)我看不到它被初始化了。

此外,请确保启用“display_errors”,方法是将以下代码添加到文件顶部。通过这种方式,您可以正确调试代码,之后,评论或删除它。

ini_set('display_errors', 1);
error_reporting(E_ALL);

作为编码风格的旁注,我建议您使用EOF代替多个echo

EX: 你有:

echo "<h1 id=\"auctiontitle\">Auction Title: $title </h1>";
    echo "<p id=\auctionbio\">Auction Description: $bio</p>";
    echo "<div id=\"infobar\">";

我建议:

$pagina = <<< EOF
<h1 id="auctiontitle">Auction Title: $title </h1>
<p id=\auctionbio">Auction Description: $bio</p>
<div id="infobar">
more html...
EOF;
echo $pagina ;

更容易编写和阅读以及更多的错误证明,因为您无需逃避一切。

答案 1 :(得分:0)

这是因为在使用它们之前没有定义那些变量。使用aution.php文件顶部的error_reporting(0)或为变量分配空值,例如

$auctionnum=$title=$bio=$category=$reserve=$reserve_met=$highest_bid="";

<强>原因:

例如,

 $temp="test";
 echo $temp;

在上面的代码$temp变量中赋值。所以它不会给你任何通知。但请看下面的代码:

echo $tt;

在此代码中,$tt变量在使用之前未定义/分配任何值。所以它会发出你提到的通知。当执行以下查询时,让我们转到您的代码,在auction.php中,

$query = 'SELECT * FROM Auctions WHERE Auctionno = "'.$auctionnum.'";';

您将拥有$auctionnum$title$bio$category等变量的值, $reserve$reserve_met$highest_bid。如果查询没有记录,那么它将被取消分配,你会看到php通知。

答案 2 :(得分:0)

这是因为在使用它们之前使用未定义的变量。 请在使用错误的行上代替:

 $clean_title = $_POST['Title'];
 $clean_bio = $_POST['Bio'];
 ....

请使用

$clean_title = (isset($_POST['Title'])?$_POST['Title']:"");
$clean_bio = (isset($_POST['Bio'])?$_POST['Bio']:"");
....

它也可以用于其他变量,例如: 而不是

$title

使用

(isset($title)?$title:"")