gpt4 book ai didi

PHP脚本添加新记录,但字段都是空白

转载 作者:行者123 更新时间:2023-11-29 06:48:34 25 4
gpt4 key购买 nike

我正在利用 html 表单和 php 将数据添加到 MySQL 数据库。我已经能够建立连接,并让 php 脚本向数据库添加新记录,但是,所有字段始终为空。我已经查看了这里和其他地方的解决方案,重做了几次脚本,但仍然遇到了同样的问题。

任何帮助将不胜感激,php 对我来说是新的。

HTML

<label for="custFirstName">First Name</label>
<input type="text" id="custFirstName" placeholder="John">

<label for="custLastName">Last Name</label>
<input type="text" id="custLastName" placeholder="Doe">

<label for="custStreet">Street Address</label>
<input type="text" id="custStreet" placeholder="1234 Cedar Street">

<label for="custCity">City</label>
<input type="text" id="custCity" placeholder="Beverly Hills">

<label for="custState">State</label>
<select id="custState">
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="DC">DC</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
</select>

<label for="custZipcode">Zip Code</label>
<input type="text" id="custZipcode" placeholder="90210">

<label for="custTX">Telephone Number</label>
<input type="text" id="custTX" placeholder="555-555-5555">

<label for="custEmail">Email Address</label>
<input type="email" id="custEmail" placeholder="name@example.com">

<button type="submit" name="submit">Submit</button>

</form>

PHP

<?php

$con = mysqli_connect('localhost','root','');

if(!con) {
echo 'Error: Not connected to the server.';
}

if(!mysqli_select_db($con,'dBaseName')) {
echo 'Error: Database is not selected';
}


if(isset($_POST['submit'])) {

$custFirstName = $_POST['custFirstName'];
$custLastName = $_POST['custLastName'];
$custStreet = $_POST['custStreet'];
$custCity = $_POST['custCity'];
$custState = $_POST['custState'];
$custZipcode = $_POST['custZipcode'];
$custTX = $_POST['custTX'];
$custEmail = $_POST['custEmail'];

$sql = "INSERT INTO Customer (custFirstName,custLastName,custStreet,custCity,custState,custZipcode,custTX,custEmail) VALUES ('$custFirstName','$custLastName','$custStreet','$custCity','$custState','$custZipcode','$custTX','$custEmail')";

if(!mysqli_query($con,$sql)) {
echo 'Error: There was a problem adding the record to the database.';
}
else {
echo 'Your information was successfully added to the database.';
}
}

header("refresh:2; url=http://www.success.html");

?>

最佳答案

您使用了错误的标签,您需要使用name而不是id。例如:

<input type="text" id="custZipcode" name="custZipcode" placeholder="90210">

关于PHP脚本添加新记录,但字段都是空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48436676/

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