简体   繁体   English

通过php将html表单数据发送到sql数据库

[英]Send html form data to sql database via php

I'm attempting to insert the data collected on the form into a mysql database. 我正在尝试将表单上收集的数据插入mysql数据库。 I'm able to make a successful connection but the data is not inserted. 我能够建立成功的连接,但未插入数据。 I've read many similar questions but have been unsuccessful so far. 我读过许多类似的问题,但到目前为止都没有成功。

sqldatabase.php sqldatabase.php

<?php
$servername = "localhost";
$username = "USER";
$password = "PASS";
$dbname = "DATABASE";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$first_spouse = $_POST['first_spouse'];
$last_spouse = $_POST['last_spouse'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phonehome = $_POST['phonehome'];
$phonecell = $_POST['phonecell'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$occupation = $_POST['occupation'];
$shirt_size = $_POST['shirt_size'];
$cap_size = $_POST['cap_size'];
$shirtnum1 = $_POST['shirtnum1'];
$shirtnum2 = $_POST['shirtnum2'];
$desc = $_POST['desc'];
$bylaws_rules = $_POST['bylaws_rules'];
$umpires = $_POST['umpires'];
$alcohol = $_POST['alcohol'];
$waiver = $_POST['waiver'];


$sql="INSERT INTO 'softball_reg_2016' (first_name, last_name, first_spouse, last_spouse,
 address, city, state, zip, phonehome, phonecell, email, dob, occupation, shirt_size,
  cap_size, shirtnum1, shirtnum2, desc, bylaws_rules, umpires, alcohol, waiver)
  VALUES ('$_POST[first_name]', '$_POST[last_name]', '$_POST[first_spouse]', '$_POST[last_spouse]',
 '$_POST[address]', '$_POST[city]', '$_POST[city]', '$_POST[state]', '$_POST[zip]',
 '$_POST[phonehome]', '$_POST[phonecell]', '$_POST[email]', '$_POST[dob]', '$_POST[occupation]',
 '$_POST[shirt_size]', '$_POST[cap_size]', '$_POST[shirtnum1]', '$_POST[shirtnum2]',
 '$_POST[desc]', '$_POST[bylaws_rules]', '$_POST[umpires]', '$_POST[alcohol]', '$_POST[waiver]')";

echo "Connected successfully";

mysqli_close($conn);
?> 

My html 我的HTML

<form action="/php/sqldatabase.php" method="POST" id="registration">

       <h2>Registration for 2016 Summer Season (April-September)</h2>  
    <p>
        <label for="name">Name:</label>
        <input type="text" id="first_name" name="first_name" placeholder="First Name" autofocus="" />
        <input type="text" id="last_name" name="last_name" placeholder="Last Name" />
    </p>

    <p>
        <label for="spouse">Name of Spouse<i>(Optional)</i>:</label>
        <input type="text" id="first_spouse" name="first_spouse" placeholder="First Name" />
        <input type="text" id="last_spouse" name="last_spouse" placeholder="Last Name" />
    </p>

    <p>
        <label for="address1">Address:</label>
        <input type="text" id="address" name="address" placeholder="Street Address" />
        <input type="text" id="city" name="city" placeholder="City" />
    </p>

    <p>
        <label for="address2"></label>
        <input type="text" id="state" name="state" placeholder="State" />
        <input type="number" id="zip" name="zip" placeholder="Zip Code" />
    </p>

    <p>
        <label for="phone">Phone:</label>
        <input type="tel" id="phonehome" name="phone" placeholder="Home Phone" />
        <input type="tel" id="phonecell" name="phone" placeholder="Work/Cell Phone" />
    </p>

    <p>
        <label for="phone">Email:</label>
        <input type="email" id="email" name="email" />
    </p>

    <p>
        <label for="dob">Date of Birth:</label>
        <input type="date" id="dob" name="dob" />
    </p>

    <p>
        <label for="occupation">Occupation (Former, if retired):</label>
        <input type="text" id="occupation" name="occupation" />
    </p>

    <div id="shirt">
    <p>
        <label for="size">Uniform:</label>
        <select name="shirt_size" id="shirt_size">
            <option value="">Shirt Size</option>
            <option value="medium">Medium</option>
            <option value="large">Large</option>
            <option value="XL">XL</option>
            <option value="2XL">2XL</option>
            <option value="3XL">3XL</option>
        </select>
        <select name"cap_size" id="cap_size">
            <option value="">Cap Size</option>
            <option value="XS/S">XS/S</option>
            <option value="S/M">S/M</option>
            <option value="M/L">M/L</option>
            <option value="L/XL">L/XL</option>
        </select>
    </p>

    <p>
       <label for="shirtnum">Shirt Number:</label>
       <input type="number" id="shirtnum1" name="shirtnum1" placeholder="1st Choice" min="0" max="99" />
       <input type="number" id="shirtnum2" name="shirtnum2" placeholder="2nd Choice" min="0" max="99" />
    </p>
    </div>
    <div id="describe">

    <p>
      <span class="describe1">
        <b>Describe any information you deem important regarding your ability and/or availability or any other information you deem important to the season.</b>
        <textarea name="description" id="desc" cols="30" rows="10"></textarea>
      </span>
    </p>
    </div>

    <div id="ethics">
        <h2>Code of Ethics</h2>
        <p>
            <span class="ethics1">
            <input type="checkbox" id="bylaws_rules" name="bylaws_rules" /> 
                I agree to abide by the Bylaws and decisions of the Club and Club Officials.
            </span>
        </p>

        <p>
            <span class="ethics1">
            <input type="checkbox" id="umpires" name="umpires" />
                I agree to accept the decisions of the Umpires and Team Managers.
            </span>
        </p>

        <p>
            <span class="ethics1">
            <input type="checkbox" id="alcohol" name="alcohol" />
                I agree to abstain from alcoholic beverages prior to a game.
            </span>
        </p>
        </div>

        <div id="waiver">
            <h2>Release of Liability</h2>

                <p>
                <b>I agree to hold harmless the club.</b>
                </p>
                <input type="checkbox" id="waiver" name="waiver" />
        </div>

        <ol class="requires">

            <li>Dues are $95 and should be received by April 6, 2016</li>
            <li>If you decline to play after being drafted, your registration fee will not be refunded.</li>
            <li>All members must be at least 50 years old by December 31, 2016</li>
            <li>The deadline for receipt of registrations is April 6, 2016. Registrations received after this date will 
                not be processed for the player drat. Assignments to teams will then be made according to League guidelines
                regarding late registering players.</li>
            <li>Registrations received without the correct fee will not be considered as received and will not be valid until the correct fee is received.</li>
        </ol>

        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>
            <button type="submit" id="register">Register!</button>
        </p>

    </form>

Thank you for any help! 感谢您的任何帮助!

Posting as a community wiki. 作为社区Wiki发布。

There are a few things wrong here. 这里有些错误。

  • First, you never executed the query. 首先,您从未执行过查询。
  • You never check for empty() fields, which might insert empty rows on the table. 您永远不会检查empty()字段,否则可能会在表上插入空行。

Consult the manual: 查阅手册:

Object oriented style 面向对象的风格

mixed mysqli::query ( string $query [, int $resultmode = MYSQLI_STORE_RESULT ] ) 混合mysqli :: query(字符串$ query [,int $ resultmode = MYSQLI_STORE_RESULT])

Procedural style 程序风格

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] ) 混合mysqli_query(mysqli $ link,字符串$ query [,int $ resultmode = MYSQLI_STORE_RESULT])

Then you're using the wrong identifier qualifiers for your table: 然后,您为表使用了错误的标识符限定符:

INSERT INTO 'softball_reg_2016'
            ^                 ^

being regular single quotes, where it should be ticks or no quotes at all: 是常规的单引号,应该是对号或完全不加引号:

INSERT INTO `softball_reg_2016`

and that alone would have thrown you a syntax error. 仅此一项就将引发语法错误。

Read up on identifier qualifiers: 阅读标识符限定符:

Then you're using desc as a column name which is a MySQL reserved word. 然后,您将desc用作列名,这是MySQL的保留字。 That also would have thrown you an error about it. 那也将引发您关于此的错误。 So, either you rename it to something else, or wrap it in ticks. 因此,您可以将其重命名为其他名称,或将其包装起来。

`desc`

Reference: 参考:

Also check for errors: 还要检查错误:

Plus, your present code is open to SQL injection . 另外,您当前的代码可以进行SQL注入 Use mysqli_* with prepared statements , or PDO with prepared statements . mysqli_*与预处理语句一起使用 ,或将PDO预处理语句一起使用

Also, since you already declared variables to your POST arrays, why put the POST arrays in the query? 另外,由于您已经为POST数组声明了变量,为什么还要在查询中放入POST数组? Just used the variables. 刚刚使用过的变量。 You're using more code for nothing really. 您实际上在使用更多代码。

And as noted in comments: 如评论中所述:

"You also have two input fields with same name: phone . Is that intended or just the copy/paste usual problem? – FirstOne" “您还有两个输入字段,具有相同的名称: phone 。这是预期的还是仅仅是复制/粘贴的常见问题?– FirstOne”

You need to execute the query. 您需要执行查询。 You have just taken the string. 您刚刚获取了字符串。 Here is what you are missing. 这就是你所缺少的。 Put

  mysqli_query($conn,$sql);

before the success message. 成功消息之前。

You didn't excute the query 您没有执行查询

you have to do 你必须做

mysqli_query($conn,$query);

before closing the connection 在关闭连接之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM