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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题