通过PHP更新数据库 [英] Updating Database through PHP

查看:87
本文介绍了通过PHP更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知疲倦地尝试着。搜索了几十个威胁,但没有运气。我似乎在我的代码中有一个错误,我似乎发现它。



我试图用sql更新我的数据库中的记录。当我点击提交时,页面重定向到我设置的页面,以显示我尝试更新的表格的内容,但没有任何更改。

 <?php 
/ *数据库凭证。假设你正在运行默认设置(用户'root'没有密码)的MySQL
服务器* /
define('DB_SERVER','###');
define('DB_USERNAME','###');
define('DB_PASSWORD','###');
define('DB_NAME','###');
$ b $ *尝试连接到MySQL数据库* /
$ mysqli = new mysqli(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);

//检查连接
if($ mysqli === false){
die(ERROR:Could not connect。。$ mysqli-> connect_error);



$ result = mysqli_query($ mysqli,SELECT * FROM appeals);
回声<文章>;

echo< table class ='w3-table-all w3-card-4'width ='700'height ='300'align ='center'border ='1'>
< caption>< h3> Appeals< / h3>< / caption>
< tr>
上诉ID< / b>
< th> ;犯罪ID第b个b $ b个归档日期第
个听证日期第
个状态第{$>
< th>更新记录< / th>
< / tr>;

while($ row = mysqli_fetch_array($ result))
{
echo< tr>;
回显< td> 。 $ row ['appeal_ID']。 < / TD> 中;
回显< td> 。 $ row ['crime_ID']。 < / TD> 中;
回显< td> 。 $ row ['filing_date']。 < / TD> 中;
回显< td> 。 $ row ['hearing_date']。 < / TD> 中;
回显< td> 。 $ row ['status']。 < / TD> 中;
echo< a href ='update-appeals.php?id =。 $ row ['appeal_ID']。 > UPDATE< / A>< / TD>中;
回声< / tr>;
}
echo< / table>;

echo< / article>;

echo< footer>
< p>创建者:Micah George< / p>
< / footer>;
echo< / body>< / html>;

mysqli_close($ mysqli);
?>

删除一些不必要的代码,以缩短发布的目的,但上面发送的ID /主键记录将更新到下面进行更新的页面。 (我知道它是有效的,因为我得到了id,但是我将它包含在需要的情况下。)

 <?php 
/ *数据库凭证。假设你正在运行默认设置(用户'root'没有密码)的MySQL
服务器* /
define('DB_SERVER','###');
define('DB_USERNAME','###');
define('DB_PASSWORD','###');
define('DB_NAME','###');
$ b $ *尝试连接到MySQL数据库* /
$ mysqli = new mysqli(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);

//检查连接
if($ mysqli === false){
die(ERROR:Could not connect。。$ mysqli-> connect_error);

$ id = $ _GET ['id'];
if(isset($ _ POST ['save']))
{
$ sql =UPDATE appeals SET crime_ID ='。$ _ POST [crime_ID]。',filing_date ='。$ _ POST [filing_date]。',hearing_date ='。$ _ POST [hearing_date]。'',status ='。$ _ POST [status]。''WHERE appeal_ID =' $ id';

$ result = mysqli_query($ mysqli,$ sql);
header('Location:jailAW-read_appeals.php');
}


echo< article>;
echo
< div class ='form'>
< form action ='update-appeals.php'method ='post'>
< label id ='first'>申诉ID:$ id< / label>< br />
< br /> $ b< input type ='hidden'name ='appeal_ID'>< br />


< label id ='first'> Crime ID:< / label>< br />
< select name ='crime_ID'>;
$ result = mysqli_query($ mysqli,SELECT crime_ID,criminal_ID FROM crimes);
while($ row = $ result-> fetch_assoc()){
$ id1 = $ row ['crime_ID'];
$ criminal = $ row ['criminal_ID'];
echo'< option value =''。$ id1。'>'。$ id1。' - ('。$ criminal。')< / option>';
}
echo
< / select>< br />


< label id ='first'>归档日期< ; / label>>< br />
< input type ='date'name ='filing_date'>< br />

< label id ='first '>听觉日期< / label>< br />
< input type ='date'name ='hearing_date'>< br />

< label id ='first'>状态:< / label>< br />
< select name ='status'>
< option value ='P'> P< ; / option>
< / select>< br />< br />

< button type ='submit'name ='save'style ='width :205px; background-color:green;'>提交< / form>;
< / div>
< / form>;
回显< / article>;

echo< footer>
< p>创建者:Micah George< / p>
< / footer>;
echo< / body>< / html>;

mysqli_close($ mysqli);
?>

新的这个,所以我的错误可能很简单,但任何帮助表示赞赏。 (对于最终的项目)。

解决方案

问题在这一行

 < form action ='update-appeals.php'method ='post'> 

您的表单提交至 update-appeals.php URL中没有 $ _ GET ['id']



我建议您将呼吁标识添加为一个隐藏的 input 给你的表单(id可以通过 $ _ POST ['id'] ):

 < select name ='status'> 
< option value ='P'> P< / option>
< / select>< br />< br />
< input type =hiddenname =idvalue =<?= $ _ GET ['id']?> />

或修改动作url(id将会以 $ _ GET ['id '] ):

 < form action ='update-appeals.php?id =< ;?= $ _ GET ['id']?>'method ='post'> 

当然,您还可以移至准备好的语句 ASAP


Been trying tirelessly. Searched dozens of threats and no luck. I seem to have an error in my code and I seem to find it.

I'm trying to update a record in my database using sql. When I hit submit the page redirects to the page I set up to display the contents of the table I am trying to update but there is no change.

<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', '###');
define('DB_USERNAME', '###');
define('DB_PASSWORD', '###');
define('DB_NAME', '###');

/* Attempt to connect to MySQL database */
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

// Check connection
if($mysqli === false){
    die("ERROR: Could not connect. " . $mysqli->connect_error);


    }
    $result = mysqli_query($mysqli,"SELECT * FROM appeals");
echo "<article>";

echo "<table class='w3-table-all w3-card-4' width='700' height='300' align='center' border='1'>
<caption><h3>Appeals</h3></caption>
<tr>
<th>Appeal ID</th>
<th>Crime ID</th>
<th>Filing Date</th>
<th>Hearing Date</th>
<th>Status</th>
<th>Update Record</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['appeal_ID'] . "</td>";
echo "<td>" . $row['crime_ID'] . "</td>";
echo "<td>" . $row['filing_date'] . "</td>";
echo "<td>" . $row['hearing_date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td style='text-align:center;'><a href='update-appeals.php?id=". $row['appeal_ID'] . "'>UPDATE</a></td>";
echo "</tr>";
}
echo "</table>";

echo "</article>";

echo "<footer>
  <p>Created by: Micah George </p>
</footer>";
echo "</body></html>";

mysqli_close($mysqli);
?>

Cut out some unneeded code for the purpose of shortening post but above sends the id/primary key of the record to be updated to the page that does the updating below. (I know it works because i get the id but I included it in case its needed.)

<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', '###');
define('DB_USERNAME', '###');
define('DB_PASSWORD', '###');
define('DB_NAME', '###');

/* Attempt to connect to MySQL database */
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

// Check connection
if($mysqli === false){
    die("ERROR: Could not connect. " . $mysqli->connect_error);

$id= $_GET ['id'];
if(isset($_POST['save']))
{
    $sql = "UPDATE appeals SET crime_ID = '".$_POST["crime_ID"]."', filing_date = '".$_POST["filing_date"]."', hearing_date = '".$_POST["hearing_date"]."', status = '".$_POST["status"]."' WHERE appeal_ID = '$id'" ;

    $result = mysqli_query($mysqli,$sql);
    header('Location: jailAW-read_appeals.php');
}


echo "<article>";
echo "
<div class='form'>
<form action='update-appeals.php' method='post'>
<label id='first'> Appeal ID: $id </label><br/>
<input type='hidden' name='appeal_ID'><br/>


<label id='first'>Crime ID:</label><br/>
<select name='crime_ID'>";
$result = mysqli_query($mysqli, "SELECT crime_ID, criminal_ID FROM crimes");
while ($row = $result->fetch_assoc()) {
    $id1 = $row['crime_ID'];
    $criminal = $row['criminal_ID']; 
    echo '<option value="'.$id1.'">'.$id1.' - ('.$criminal.')</option>';
}
echo"
</select><br/>


<label id='first'>Filing Date</label><br/>
<input type='date' name='filing_date'><br/>

<label id='first'>Hearing Date</label><br/>
<input type='date' name='hearing_date'><br/>

<label id='first'>Status:</label><br/>
<select name='status'>
  <option value='P'>P</option>
</select><br/><br/>

<button type='submit' name='save' style='width:205px; background-color: green;'>Submit</button>
</div>
</form>";
echo "</article>";

echo "<footer>
  <p>Created by: Micah George </p>
</footer>";
echo "</body></html>";

mysqli_close($mysqli);
?>

New to this so my mistake might be simple but any help is appreciated. (For a final project).

解决方案

Problem is in this line

<form action='update-appeals.php' method='post'>

Whe your form submits to update-appeals.php there's no $_GET['id'] in URL.

I advise you to add appeal id as a hidden input to your form (id will be available via $_POST['id']):

<select name='status'>
  <option value='P'>P</option>
</select><br/><br/>
<input type="hidden" name="id" value="<?=$_GET['id']?>" />

Or modify action url (id will be available as $_GET['id']):

<form action='update-appeals.php?id=<?=$_GET['id']?>' method='post'>

And of course move to prepared statements ASAP.

这篇关于通过PHP更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆