gpt4 book ai didi

php - 将HTML表单的输入数据插入JSON对象,然后将其存储在MYSQL中

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

我试图从 HTML 格式的用户注册表中获取数据,然后将数据推送到 JSON,然后获取 JSON 并将其存储到 MySQL 中。请帮我。

HTML

    <form id="myForm" action="userInfo.php" method="post">
<table align="center">
<tr>
<td><label for="FirstNameLabel" class="tableproperties">First Name</label></td>
<td><input type="text" class="signupTextBoxStyle" name="firstName" placeholder="Enter First Name" id="FirstNameBox" required/></td>
</tr>
<tr>
<td><label for="LastNameLabel" class="tableproperties">Last Name</label></td>
<td><input type="text" name="lastName" placeholder="Enter Last Name" id="LastNameBox" class="signupTextBoxStyle" required></td>
</tr>
<tr>
<td><label for="eMailLabel" class="tableproperties">Email</label></td>
<td><input type="email" name="email" placeholder="Enter Email" id="eMailBox" class="signupTextBoxStyle" required></td>
<td id="emailStatus"></td>
</tr>

<tr>
<td><label for="passwordLabel" class="tableproperties">Password</label></td>
<td><input type="password" name="password" placeholder="Enter Password" id="passwordTextbox" maxlength="24" class="signupTextBoxStyle" required></td>
<td><i class="fa fa-info-circle infoIcon" title="Password must contain minimum 3 upper case, 2 lower case and 2 special chars"></i></td>
<td><progress value="0" max="100" class="progressBar" id="progressStatus"></progress></td>
<td id="passwordStrength"></td>
</tr>

<tr>
<td><label for="confirmPasswordLabel" class="tableproperties">Confirm Password</label></td>
<td><input type="password" name="confirmpassword" placeholder="Must be same as password" maxlength="24" id="confirmPasswordBox" class="signupTextBoxStyle" required></td>
<td id="passwordMismatch"></td>
</tr>

<tr>
<td><label for="dobLabel" class="tableproperties">D.O.B</label></td>
<td><input type="date" name="dob" placeholder="Enter D.O.B" id="dobBox" class="signupTextBoxStyle" required></td>
</tr>

<tr>
<td><label for="dobTimeLabel" class="tableproperties">D.O.B with time</label></td>
<td><input type="datetime" name="dobTime" placeholder="Enter D.O.B with time" id="dobTimeBox" class="signupTextBoxStyle" required></td>
</tr>

<tr>
<td><label for="localDOBLabel" class="tableproperties">Local D.O.B</label></td>
<td><input type="datetime-local" name="localdob" placeholder="Enter Local D.O.B" id="localDobBox" class="signupTextBoxStyle" required></td>
</tr>

<tr>
<td><label for="ssnLabel" class="tableproperties">SSN</label></td>
<td><input type="text" name="ssn" placeholder="000-00-0000" id="ssnBox" class="signupTextBoxStyle" required pattern="^(\d{3}-\d{2}-\d{4})$"></td>
</tr>

<tr>
<td><label for="usPhoneNumber" class="tableproperties" >US Phone Number</label></td>
<td><input type="text" name="phone" placeholder="000-000-0000" id="usNumberBox" class="signupTextBoxStyle" required></td>
<td id="phoneStatus"></td>
</tr>

<tr>
<td><label for="creditLabel" class="tableproperties" id="CreditText">Credit Card Number</label></td>
<td><input type="text" name="creditCardNumber" placeholder="Enter Credit Card Number" id="creditBox" class="signupTextBoxStyle" required pattern="^[0-9]{12}(?:[0-9]{4})?$"></td>
</tr>


<tr>
<td colspan='2'>
<input type="submit" class="btn btn-primary btn-lg btn-block signupbuttonStyle" id="sub" />
<button type="button" class="btn btn-danger btn-lg btn-block signupbuttonStyle" onclick="location.href = 'index.html';">Cancel</button>
</td>
</tr>
</table>
</form>

PHP(只是为了测试如果我手动输入数据,数据是否会保存到 mySQL)

$json_obj = '{
"jsonFirstName": "Kishan",
"jsonLastName": "Kishan",
"jsonEmail": "Kishan",
"jsonPassword": "Kishan",
"jsonDob": "Kishan",
"jsonDobTime": "Kishan",
"jsonLocaldob": "Kishan",
"jsonSsn": "Kishan",
"jsonPhonenumber": "Kishan",
"jsonCreditcardnumber": "Kishan"
}';

PHP(如果我想从表单中获取值,则会出错)

$json_obj = '{
"jsonFirstName": (string) $_POST['firstName'],
"jsonLastName": (string) $_POST['lastName'],
"jsonEmail": (string) $_POST['email'],
"jsonPassword": (string) $_POST['password'],
"jsonDob": (string) $_POST['dob'],
"jsonDobTime": (string) $_POST['dobTime'],
"jsonLocaldob": (string) $_POST['localdob'],
"jsonSsn": (string) $_POST['ssn'],
"jsonPhonenumber": (string) $_POST['phone'],
"jsonCreditcardnumber": (string) $_POST['creditCardNumber']
}';

错误描述
解析错误:语法错误,第 19 行/Applications/XAMPP/xamppfiles/htdocs/xampp/297test/userInfo.php 中出现意外的“firstName”(T_STRING)

PHP 代码的其余部分
$结果 = json_decode($json_obj);

$firstname = $result->jsonFirstName;
$lastname = $result->jsonLastName;
$email = $result->jsonEmail;
$password = $result->jsonPassword;
$dob = $result->jsonDob;
$dobTime = $result->jsonDobTime;
$localdob = $result->jsonLocaldob;
$ssn = $result->jsonSsn;
$phonenumber = $result->jsonPhonenumber;
$creditcardnumber = $result->jsonCreditcardnumber;


if(mysql_query("INSERT INTO user VALUES('$firstname', '$lastname', '$email', '$password', '$dob', '$dobTime', '$localdob', '$ssn','$phonenumber','$creditcardnumber')")){
echo "Successfully Inserted";
}

else
echo "Fail to Insert";

最佳答案

由于引号、换行符等原因,直接通过连接创建 JSON 字符串很困难。

相反,创建一个值数组并使用 json_encode 将其编码为 JSON 字符串:

$values = array(
"jsonFirstName" => $_POST['firstName'],
"jsonLastName" => $_POST['lastName'],
"jsonEmail" => $_POST['email'],
"jsonPassword" => $_POST['password'],
"jsonDob" => $_POST['dob'],
"jsonDobTime" => $_POST['dobTime'],
"jsonLocaldob" => $_POST['localdob'],
"jsonSsn" => $_POST['ssn'],
"jsonPhonenumber" => $_POST['phone'],
"jsonCreditcardnumber" => $_POST['creditCardNumber']
);

$json_obj = json_encode($values);

或者你可以这样做:

$json_obj = json_encode($_POST);

然后您将获得一个 JSON 对象,其中每个索引为 $_POST。唯一的区别是,您无法像示例中那样重命名字段。

关于php - 将HTML表单的输入数据插入JSON对象,然后将其存储在MYSQL中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26229547/

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