【问题标题】:Insert input data from HTML form into JSON object and then store it in MYSQL将 HTML 表单中的输入数据插入 JSON 对象,然后将其存储在 MYSQL 中
【发布时间】:2014-12-01 11:58:18
【问题描述】:

我正在尝试从 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 代码的 REST
$result = 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?
  • 我不明白您为什么需要将 POST 变量转换为 json 字符串,然后对其进行解码,然后再次使用它。这没有意义
  • $json_obj = "{'jsonFirstName': " 。 mysqli_real_escape_string($_POST['firstName'] . ")} - 或者更好的是,使用准备好的语句...
  • 我只是在我的大学做作业..这些是要求...我需要在 json 中获取表单输入,然后将其解析到 SQL server...
  • @Fabian 它仍然没有工作。感谢您的输入:(

标签: php html mysql ajax json


【解决方案1】:

不知道为什么要这样做,但是解析错误是因为您使用单引号打开字符串,然后还使用它来选择数组索引。

$_POST['firstName']更改为使用双引号,例如$_POST["firstName"]

【讨论】:

  • 它仍然没有 wrk..this 错误出现在注意:尝试在第 35 行的 /Applications/XAMPP/xamppfiles/htdocs/xampp/297test/userInfo.php 中获取非对象的属性“$ firstname = $result->jsonFirstName;"-->它指出的这一行
【解决方案2】:

编辑: 尝试这样做。

$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 = json_encode($array);

【讨论】:

  • 仍然没有运气......现在的错误是“解析错误:语法错误,第 18 行 /Applications/XAMPP/xamppfiles/htdocs/xampp/297test/userInfo.php 中的意外 '{'”
  • 解析错误:语法错误,/Applications/XAMPP/xamppfiles/htdocs/xampp/297test/userInfo.php 中的意外'=>' (T_DOUBLE_ARROW) ..我收到这个新错误:(
  • 你忘了从$array=array([...]);开始
【解决方案3】:

您收到错误是因为您以错误的方式从 POST 变量中创建了 JSON 对象。您的声明中的引号确实存在问题。它应该是这样的:

  $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'].'"
  }';

如果我在构造 JSON 对象时只包含不带双引号的变量,我会得到类似:

  $name = "Foo";
  $json_obj = '{"firstName" :'.$name.'}'; 

 //gets expanded to $json_obj = {"firstname" : Foo}
 //Whereas it should be $json_obj = {"firstname" :"Foo"}

这是由于缺少双引号,您无法首先创建 JSON 对象,因此当您尝试对其进行解码并进一步访问属性时,它给出了错误提示:

 Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/xampp/297test/userInfo.php on line 35

试试我给出的代码 sn-p,它应该可以帮助你。

【讨论】:

    【解决方案4】:

    由于引号、换行符等,直接通过连接创建 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。唯一的区别是,您不能像在示例中那样重命名字段。

    【讨论】:

    • 谢谢......到目前为止它在 gud 上的 gng......当我回显 $json_obj..我得到以下结果({“jsonFirstName”:“userfirst”,“jsonLastName”:“userlast ","jsonEmail":"user@gmail.com","jsonPassword":"QWERqwer!@#$","jsonDob":"2014-01-01","jsonDobTime":"dobtime","jsonLocaldob": "2014-01-01T01:00","jsonSsn":"123-12-1234","jsonPhonenumber":"123-123-1234","jsonCreditcardnumber":"123412341234"})...请指导我如何将这些值一个一个地存储在 mySQl 服务器中...实际上可以解决我的问题..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2015-09-28
    相关资源
    最近更新 更多