gpt4 book ai didi

PHP无法写入MySQL数据库

转载 作者:行者123 更新时间:2023-11-29 08:16:15 25 4
gpt4 key购买 nike

我已经检查了我的代码很多很多次,并添加了任何缺少的括号或分号,但每当我将此代码上传到我的网站并加载页面时,我仍然得到一个完全空白的屏幕。该代码来自 O'Reilly 的一本书籍,所以我去检查了网站,看看书中是否有任何报告的错误,但没有发现与这个特定示例相关的任何内容。

我不认为这是权限问题,因为我认为该页面至少会报告我编码到其中的错误之一。这可能与我使用的 PHP 或 MySQL 版本有关吗?我过去能够连接到数据库并查询它,但写入却没有发生。我现在完全不知所措。我想做的就是写入我的 MySQL 数据库和派对:(

这是代码:

    <?php
require_once 'login.php';

// Create connection
$db_server = mysql_connect($db_hostname, $db_username, $db_password);

// Check connection
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['delete']) && isset($_POST['avail']))
{
$avail = get_post('avail');
$query = DELETE FROM test WHERE avail='$avail';

if (!mysql_query($query, $db_server))
echo "DELETE failed: $query<br />" .
mysql_error() . "<br /><br />";
}

if (isset($_POST['id']) &&
isset($_POST['item_name']) &&
isset($_POST['avail']))
{
$id = get_post('id');
$item_name = get_post('item_name');
$avail = get_post('avail');

$query = "INSERT INTO test VALUES" .
"('$id','$item_name','$avail')";

if (!mysql_query($query, $db_server))
{echo "INSERT failed: $query<br />" .
mysql_error() . "<br /><br />";
}
}

?>
<form action="index.php" method="post"><pre>
Line Number <input type="text" name="id" />
Product Name <input type="text" name="item_name" />
Quantity Available <input type="text" name="avail" />
<input type="submit" value="ADD RECORD" />
</pre></form>
<?php


$query = "SELECT * FROM test";
$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);
?>
<pre>
Line Number $row[0]
Product Name $row[1]
Quantity Available $row[2]
</pre>
<form action="index.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="avail" value="$row[2]" />
<input type="submit" name="DELETE RECORD" /></form>
<?php
}

mysql_close($db_server);

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



?>

最佳答案

您的删除语句有错误,请尝试以下代码:

 <?php
require_once 'login.php';

// Create connection
$db_server = mysql_connect($db_hostname, $db_username, $db_password);

// Check connection
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['delete']) && isset($_POST['avail']))
{
$avail = get_post('avail');
$query = "DELETE FROM test WHERE avail='$avail'";

if (!mysql_query($query, $db_server))
echo "DELETE failed: $query<br />" .
mysql_error() . "<br /><br />";
}

if (isset($_POST['id']) &&
isset($_POST['item_name']) &&
isset($_POST['avail']))
{
$id = get_post('id');
$item_name = get_post('item_name');
$avail = get_post('avail');

$query = "INSERT INTO test VALUES" .
"('$id','$item_name','$avail')";

if (!mysql_query($query, $db_server))
{echo "INSERT failed: $query<br />" .
mysql_error() . "<br /><br />";
}
}

?>
<form action="index.php" method="post"><pre>
Line Number <input type="text" name="id" />
Product Name <input type="text" name="item_name" />
Quantity Available <input type="text" name="avail" />
<input type="submit" value="ADD RECORD" />
</pre></form>
<?php


$query = "SELECT * FROM test";
$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);
?>
<pre>
Line Number $row[0]
Product Name $row[1]
Quantity Available $row[2]
</pre>
<form action="index.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="avail" value="$row[2]" />
<input type="submit" name="DELETE RECORD" /></form>
<?php
}

mysql_close($db_server);

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



?>

关于PHP无法写入MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20550871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com