gpt4 book ai didi

PHP/MySQL 查询不起作用

转载 作者:行者123 更新时间:2023-11-29 22:46:40 25 4
gpt4 key购买 nike

我正在尝试从表单将轮胎添加到轮胎数据库中。如果我使用 var_dump,查询是正确的,但它不会将任何内容插入数据库。这是我的代码。

 <?php

require('../includes/database_connection.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);
$idtires = '';
$size = $_POST["size"];
$idbrand = $_POST["idbrand"];
$price = $_POST["price"];
$mileage = $_POST["mileage"];
$idterrain = $_POST["idterrain"];
$idvehicle = $_POST["idvehicle"];
$idtiremodel = $_POST["idtiremodel"];

function add_tire($idtires, $size, $idbrand, $price, $mileage, $idterrain, $idvehicle, $idtiremodel) {
global $db;
$query = "INSERT INTO tires
(idtires, size, idbrand, price, mileage, idterrain, idvehicle, idtiremodel)
VALUES
($idtires, '$size', '$idbrand','$price', '$mileage', '$idterrain', '$idvehicle', '$idtiremodel')";
var_dump($query);
// var_dump($db);
$db->exec($query);

}


add_tire($idtires, $size, $idbrand, $price, $mileage, $idterrain, $idvehicle, $idtiremodel);



include('add.php');

?>

这里是 ADD.PHP 表单:

<table>

<form method='post' action='addatire.php' name='addform' id='addform'>
<tr>
<td>
<p>
<label for='size'>Size:</label><br />
</p>
</td>
<td>
<p>
<input type='text' name='size' required value="<?php echo(isset($size['size'])); ?>"/>
</p>
</td>
</tr>
<tr>
<td>
<p>
<label for='idbrand'>Brand ID:</label><br/>
</p>
</td>
<td>
<p>
<input type='text' name='idbrand' required value="<?php echo(isset($idbrand['idbrand'])); ?>"/>
</p>
</td>
</tr>
<tr>
<td>
<p>
<label for='price'>Price:</label><br/>
</p></td>
<td><p>
<input type='text' name='price' required value="<?php echo(isset($price['price'])); ?>"/>
</p></td>
</tr>
<tr>
<td>
<p>
<label for='mileage'>Mileage:</label><br/>
</p></td>
<td><p>
<input type='text' name='mileage' required value="<?php echo(isset($mileage['mileage'])); ?>"/>
</p></td>
</tr>
<tr>
<td>
<p>
<label for='idterrain'>Terrain ID:</label><br/>
</p></td>
<td><p>
<input type='text' name='idterrain' required value="<?php echo(isset($idterrain['idterrain'])); ?>"/>
</p></td>
</tr>
<tr>
<td>
<p>
<label for='idvehicle'>Vehicle ID:</label><br/>
</p></td>
<td><p>
<input type="text" name="idvehicle" required value="<?php echo(isset($idvehicle['idvehicle'])); ?>"/>

</p></td>
</tr>

<tr>
<td>
<p>
<label for='idtiremodel'>Tire Model ID:</label><br/>
</p></td>
<td><p>
<input type="text" name="idtiremodel" required value="<?php echo(isset($idtiremodel['idmodel'])); ?>"/>

</p></td>
</tr>



<tr>
<td><p>
<input type="submit" name="action" value="Add A Tire"/>

</p></td>
</tr>
</form>
</table>



<?php echo get_tires(); ?>
</div>


?>

这是数据库连接:

<?php
if ($_SERVER['HTTP_HOST'] == "localhost" OR $_SERVER['HTTP_HOST'] == "127.0.0.1") {
// login locally
$dsn = 'mysql:host=localhost;dbname=?';
$username = ?;
$password = ?;

} else {
// login online
$dsn = 'mysql:host=localhost;dbname=?';
$username = '?';
$password = '?';

}

try {
$db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();

}
?>

我的用户名和密码不是真的吗?不过,我在这里使用这些只是为了隐私和安全。

最佳答案

您可能在插入内容时遇到错误,但只是没有看到它:

$db->exec($query) or die(print_r($db->errorInfo(), true));

关于PHP/MySQL 查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29069310/

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