简体   繁体   English

更新SQL表

[英]Update SQL tables

I want to update a table on a specific row need some advice on my php statement 我想更新特定行上的表,需要有关我的php语句的一些建议

I use this statement to call the client's info 我使用此语句来调用客户的信息

<?php
$host="localhost"; // Host name 
$username="****"; // Mysql username 
$password="****"; // Mysql password 
$db_name="****"; // Database name 
$tbl_name="members"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];

// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE member_msisdn='$query'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>

This works fine echoíng the information I need and able to alter it. 这很好地反映了我需要的信息并能够更改它。

<form name="form1" method="post" action="control_clientupdated.php">

This referes to my action php script 这是指我的动作php脚本

Problem I need assistance with is how do i notify my action script to use the same id I ran the query on to update that row. 我需要帮助的问题是如何通知我的动作脚本使用与运行查询以更新该行相同的ID。

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database 
$sql="UPDATE $tbl_name SET member_name='$member_name',     
member_surname='$member_surname', member_msisdn='$member_msisdn', cid='$cid',    
cofficenr='$cofficenr', cfax='$cfax',  e2mobile='$e2mobile' WHERE member_msisdn='$query'";
$result=mysql_query($sql);

I have placed the WHERE statement on the end of the update 我已将WHERE语句放在更新的结尾

Let me just state it shows done, but it did not update the table at all 我只说它显示完成,但是它根本没有更新表

Firstly you need to store your ID into a hidden form element in your form. 首先,您需要将ID存储到表单中的隐藏表单元素中。

<form method="post" action="control_clientupdated.php">
    <input type="hidden" name="member_msisdn" value="<?=$query?>" />
    ...
</form>

This will allow you to passthrough the value from your first php script. 这将允许您从第一个php脚本中传递值。

Then in your control_clientupdated.php you need to use $_POST to recover your value. 然后,在您的control_clientupdated.php您需要使用$_POST来恢复您的价值。

// Store the $_POST value for my query ID
$query = $_POST['member_msisdn'] ;

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database 
$sql="UPDATE $tbl_name SET member_name='$member_name',     
member_surname='$member_surname', member_msisdn='$member_msisdn', cid='$cid',    
cofficenr='$cofficenr', cfax='$cfax',  e2mobile='$e2mobile' WHERE member_msisdn='$query'";
$result=mysql_query($sql);

This should be what you need - note that you cannot use $_GET to retrieve the variable passed by the form, as you are sending it with the method="post" attribute, you must use $_POST instead of $_GET 这应该是您所需要的-请注意,您不能使用$_GET来检索由表单传递的变量,因为要通过method="post"属性发送该变量,所以必须使用$_POST而不是$_GET

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

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