donglun4521 2016-08-08 10:23
浏览 50

使用php将mysql数据加载到html表单和updata

I extremely new to both mysql and php (mostly mysql), and i'm not even sure i got all of the basics, cause i'm more of a learning-by-doing-person. So my code is made up of different explanations all around the web, and some of it might be wrong. So i'm trying to get data from a database, put it into a html form, edit it, and update the database with the new data. So far the data gets loaded into the form correctly, and with a lot of troubleshooting i've come to the conclusion that even the upload works out as it should, except the fields are empty. So i guess that my problem must be in getting the data from the form, to the update query.

This is my code

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<?php
require 'connect.php';


$result = $db->query("SELECT * FROM test") or die ($db->error);

while($row = mysqli_fetch_array($result))
{

    $name = $row['name'];
    $school = $row['school'];
    $email = $row['email'];
    $grade = $row['grade'];
    $workshop = $row['workshop'];
    $id = $row['id'];
?>
<form method="post" action="#">
    <?=$id?>
    <input name="nameid" type="text" value="<?=$name?>">
    <input name="emailid" type="text" value="<?=$email?>">
    <input name="gradeid" type="text" value="<?=$grade?>">
    <input name="workshopid" type="text" value="<?=$workshop?>">
    <input name="schoolid" type="text" value="<?=$school?>">
<?php } 
session_start();
$_SESSION['storage'] = $_POST;
?>
<input name="update" type="submit" id="update" value="Update">
</form>


<?php
if(isset($_POST['update']))
{

    $link = mysqli_connect("localhost", "892847", "123456789", "892847");

    // Check connection
    if($link === false){
        die("Couldn't connect. " . mysqli_connect_error());
    }
    session_start();

    $name = $_SESSION['storage']['nameid'];
    $school = $_SESSION['school']['schoolid'];
    $email = $_SESSION['email']['emailid'];
    $grade = $_SESSION['grade']['gradeid'];
    $workshop = $_SESSION['workshop']['grade'];
    $id = $_SESSION['id'];


    $sql = "UPDATE test SET name = '$name', email = '$email', school = '$school', grade = '$grade', workshop = '$workshop' WHERE id = '8'";
    if(mysqli_query($link, $sql)){
        echo "Records added successfully. $name";
        print_r($_POST['nameid']);
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }


    mysqli_close($link);
}
?>

</body>
</html>

I'm aware that the session-chaos looks strange, but i read somewhere that a information isn't available across PHP-scripts, unless you make a session.

I'm really grateful for any kind of help - i've been fighting with this for about 48 hours

  • 写回答

1条回答 默认 最新

  • douguang9014 2016-08-08 11:10
    关注

    This is a basic approach, using the easiest procedural php. First, you load data in from mysql into php array as follows, assuming you've already made the connections and id is the id of that specific row

    $row = mysql_fetch_array(mysql_query("SELECT * FROM test WHERE `id` = '$id'"));
    

    Find a way of ensuring that that page is displaying the required id, for instance, you can use $id = $_GET[row]; in that case your url will be http://url?row=id In the html code use echo as follows

    <form action="process.php" method="POST">
    <input type="hidden" name="id" value="<?php echo $_GET['row']" ?>" >
    <input type="text" name="nameid" value="<?php echo $row['nameid'] ?>" >
    ...
    </form>
    

    Forget about sessions, the moment you press submit it should carry all this data in to the process.php in a global variable $_POST or $_GET, depending on which method you chose in the <form method>.

    Now in the process.php, use

    $nameid = mysql_real_escape_string($_POST['nameid']);
    $id = $_POST['id'];
    $sql = mysql_query("UPDATE test SET `name_id` = '$nameid' WHERE `id` = '$id'");
    if($sql) {
    echo "update successful";
    }
    

    Needless to say, you need to use database connections in both pages.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100