drfu29983 2012-09-26 20:58
浏览 51
已采纳

通过html表单保存到MySQL数据库

I am making a php page that retrieves data from a database table and putting it in a table for the user to see via MySQLi commands.

I was wondering how I should approach the reverse situation. I want the user to be able to enter in information into textboxes and the click a button at the bottom of the page called 'save' which will prompt the user "are you sure" before saving to the database. If the user clicks 'yes', then the new entry is inserted into the database.

I have the following code to create the labels and textboxes:

<FORM>
ID: <input type="text" name="id"><br />
NM: <input type="text" name="nm"><br />
Company: <input type="text" name="company"><br />
Address: <input type="text" name="address"><br />
City: <input type="text" name="city"><br />
Zip: <input type="text" name="zip"><br />
State: <input type="text" name="state"><br />
Phone: <input type="text" name="phone"><br />
Website: <input type="text" name="web_site"><br />
</FORM>

However, when it comes to the 'save' button, I can implement the button just fine, but how would I go about saving the information entered into the database?

My initial thought process was to find the values that the user entered. I'm new to PHP and WEB dev in general, but I need to know how to get the value of the text in the textbox. Would I have to sift all the values through via the PHP Post method?

Once I have the information the user wants to enter, I was thinking maybe MySQLi has an insert function, which I found here, http://php.net/manual/en/mysqli.insert-id.php. Then it's just a quick insert and it's in the database after the user gives the 'yes' at the prompt.

Do I have the right idea in mind? Is there a more efficient way to do this?

Any help is greatly appreciated. I've looked around for problems and solutions similar to the ones related to my scenario but there were none. =(

Thanks!

EDIT:

Here is the code I have on the agentprocess.php that the action form sends the information to:

<?php
$agent_nm = $_POST['nm']; // gather all the variables
$company = $_POST['company'];
$address = $_POST['address'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$state = $_POST['state'];
$phone = $_POST['phone'];
$web_site = $_POST['web_site'];
$batch_id = $_POST['batch_id']; // added batch id
//connect
$conn = new mysqli('local', 'admin', 'pass', 'DB');
    if(mysqli_connect_errno()) {
        exit('Connect failed: '. mysqli_connect_error());
    }
//generate the query (doesn't add id because id is autoincremented)
$query = "INSERT INTO t_agent VALUES (NULL, " . $agent_nm . ", " . $company . ", " . $address . ", " . $city . ", " . $zip . ", " . $state . ", " . $phone . ", " . $web_site . ", " . $batch_id . ")";

//insert and close.
mysqli_query($conn, $query);
mysqli_close($conn);

Despite the code here, I've queried the table and the new entry is not there. Am I missing something here?

Thanks in advance!

  • 写回答

4条回答 默认 最新

  • douxin2002 2012-09-26 21:17
    关注

    Very simple example, added the label tag to the labels for your input and put it inside of a form.

    <form method="post" action="process.php" id="myForm" name="myForm" >
    <label for="ID">ID</label>:  <input type="text" name="ID" /><br />
    <label for="nm">NM:</label> <input type="text" name="nm"><br />
    <label for="company">Company:</label> <input type="text" name="company"><br />
    <label for="address">Address:</label> <input type="text" name="address"><br />
    <label for="city">City</label>: <input type="text" name="city"><br />
    <label for="zip">Zip</label>: <input type="text" name="zip"><br />
    <label for="state">State</label>: <input type="text" name="state"><br />
    <label for="phone">Phone</label>: <input type="text" name="phone"><br />
    <label for="web_site">Website</label>: <input type="text" name="web_site"><br />
    <input type="submit" name="submit" />// this is your submit button
    </form>
    

    On the process.php page

    //get your inputs from the form
    $ID = $_POST['ID']; 
    //do the same for each of the text inputs
    

    Then you can use mysqli as you described to insert the values into your database, feel free to comment if you need any help with the mysqli part of the question, I didn't include it here since you had the link posted in the original question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮