0

I do have programming experience, but new to php. I do have an issue with an example I was doing from this tutorial. I looked over it millions of times, googled, ect ect. I don't have an idea why my code isnt working.

The purpose is to basically just test inserting and deleting in sql from php, using a button for Add Record and Delete Record. The Add record button works perfectly, but delete doesnt do a thing other than reload the page. Heres the code...

<?php // sqltest.php

require_once 'login.php';

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database, $db_server)
     or die("Unable to select database: " . mysql_error());

if (isset($_POST['author'])   &&
    isset($_POST['title'])    &&
    isset($_POST['type'])     &&
    isset($_POST['year'])      &&
    isset($_POST['isbn']))
{
    $author = get_post('author');
    $title = get_post('title');
    $type = get_post('type');
    $year = get_post('year');
    $isbn = get_post('isbn');

    if (isset($_POST['delete']) && $isbn != "")
    {
        echo "worked!!!!!!!!!!!!!!"; 
        $query = "DELETE FROM classics WHERE isbn='$isbn'";
        $result = mysql_query($query) or die(mysql_error());
        if(mysql_affected_rows($result) > 0) echo 'user deleted';

        //if (!mysql_query($query, $db_server))
        //echo "DELETE failed: $query" . mysql_error();
    }
    else
    {
        echo "nooooooooooooooooooo";
        $query = "INSERT INTO classics VALUES" .
        "('$author', '$title', '$type', '$year', '$isbn')";
        if (!mysql_query($query, $db_server)) 
        {
            echo "INSERT failed: $query" . mysql_error();
        }
    }
}

echo <<<_END
<form action="sqltest.php" method="post"><pre>
Author <input type="text" name="author" />
Title  <input type="text" name="title" />
Type   <input type="text" name="type" />
Year   <input type="text" name="year" />
ISBN   <input type="text" name="isbn" />
<input type='submit' value='ADD RECORD' />
</pre></form>
_END;



$query = "SELECT * FROM classics";

$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());

$rows = mysql_num_rows($result);

for ($j = 0 ; $j < $rows ; ++$j)
{
        $row = mysql_fetch_row($result);

echo <<<_END
<pre>
Author $row[0]
Title $row[1]
Type $row[2]
Year $row[3]
ISBN $row[4]
<form action="sqltest.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name='isbn' value="$row[4]" />
<input type='submit' value='DELETE RECORD' />
</form>
</pre>
_END;
}

mysql_close($db_server);

function get_post($var)
{
    return mysql_real_escape_string($_POST[$var]);
}

?>

I have looked over this many times, still no idea why this won't work. Is it the for loop that is making this button not work? Note, you will see echo "worked!!!"; and in the else echo "noooooooo"; that was for me to test whether the button was being tested, yet nothing prints. So maybe i missed something in the button code itself? Also, no errors are printed, and my editor (and myself) have missed the syntax error (if thats the case).

The code for the delete button is at the end, before I closed the DB.

Thanks for your help in advance.

4

3 回答 3

2

你的问题是你的第一个if障碍。

您正在检查是否存在已发布的变量author title type year isbn。而在您的删除代码中,发送的唯一变量是deleteisbn。因此第一个if块完全丢失(包括删除代码)。

您需要将您的第一个修改ifif(isset($_POST)) { // a form has been posted. 然后它应该工作。

另一种方法:

if(isset($_POST['delete']) && isset($_POST['isbn']) && !empty($_POST['isbn'])){
    //delete code here
}

if(isset($_POST['author']) && isset($_POST['title']) && isset....){
    // insert code here
}

编辑:重写代码:

<?php // sqltest.php

// I don't know what's in here, so I've left it
require_once 'login.php';

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database, $db_server)
     or die("Unable to select database: " . mysql_error());

if (isset($_POST))
{

    if (isset($_POST['delete']) && !empty($_POST['isbn']))
    {
        echo "Deleting"; 
        $query = "DELETE FROM classics WHERE isbn='".mysql_real_escape_string($_POST['isbn'])."'";
        $result = mysql_query($query) or die(mysql_error());
        if(mysql_affected_rows($result) > 0) echo 'user deleted';
    }
    else
    {
        echo "Inserting";
        $query = "INSERT INTO classics VALUES ('".mysql_real_escape_string($_POST['author'])."', '".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['type'])."', '".mysql_real_escape_string($_POST['year'])."', '".mysql_real_escape_string($_POST['isbn'])."')";
        if (!mysql_query($query)) 
        {
            echo "INSERT failed: $query" . mysql_error();
        }
    }
}

// you don't need echo's here... just html
?>

<form action="sqltest.php" method="post">
    <pre>
        Author <input type="text" name="author" />
        Title  <input type="text" name="title" />
        Type   <input type="text" name="type" />
        Year   <input type="text" name="year" />
        ISBN   <input type="text" name="isbn" />
        <input type='submit' value='ADD RECORD' />
    </pre>
</form>

<?php

$query = "SELECT * FROM classics";

$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());

// a better way to do this:
while($row = mysql_fetch_array($result)){
?>

<pre>
    Author <?php echo $row[0]; ?>
    Title <?php echo $row[1]; ?>
    Type <?php echo $row[2]; ?>
    Year <?php echo $row[3]; ?>
    ISBN <?php echo $row[4]; ?>
    <form action="sqltest.php" method="post">
        <input type="hidden" name="delete" value="yes" />
        <input type="hidden" name='isbn' value="<?php echo $row[4]; ?>" />
        <input type='submit' value='DELETE RECORD' />
    </form>
</pre>

<?php
}

mysql_close($db_server);

?>
于 2012-07-01T01:03:24.707 回答
0

验证您在表单中使用的方法。确保它是这样的 POST:

形式action="yourpage.php" method="POST"

在上面的代码中,替换以下内容:

$author = get_post('author');
    $title = get_post('title');
    $type = get_post('type');
    $year = get_post('year');
    $isbn = get_post('isbn');

$author = $_POST['author'];
$title = $_POST['title'];
$type = $_POST['type'];
$year = $_POST['year'];
$isbn = $_POST['isbn'];

最后,没有必要再次检查是否$isbn不为空,就像您在isset()方法中所做的那样。$isbn!=""所以在下面的 if 中删除:

if (isset($_POST['delete']) && $isbn != "")
    {
}

变成:

if (isset($_POST['delete']))
    {
}

由于您正在测试,因此检查用户是否单击了删除按钮并不那么重要。因此,您也可以暂时删除它,稍后再添加,因为您确信在单击删除按钮后可以访问该代码。

于 2013-10-12T17:51:56.510 回答
-2

您没有名为 的表单字段delete,因此永远不可能采用您的删除代码路径。

我猜您正在尝试使用提交按钮的值来决定要做什么?在这种情况下,您还缺少name提交按钮上的属性 - 没有它,它无法使用表单提交任何值。你可能想要:

<input type="submit" name="submit" value="DELETE RECORD" />

然后有

if (isset($_POST['submit']) && ($_POST['submit'] == 'DELETE RECORD')) {
   ...
}
于 2012-07-01T01:01:43.370 回答