Loading mysql data to a html form and updata with php -
i extremely new both mysql , php (mostly mysql), , i'm not sure got of basics, cause i'm more of learning-by-doing-person. code made of different explanations around web, , of might wrong. i'm trying data database, put html form, edit it, , update database new data. far data gets loaded form correctly, , lot of troubleshooting i've come conclusion upload works out should, except fields empty. guess problem must in getting data form, update query.
this 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 * 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' id = '8'"; if(mysqli_query($link, $sql)){ echo "records added successfully. $name"; print_r($_post['nameid']); } else{ echo "error: not able execute $sql. " . mysqli_error($link); } mysqli_close($link); } ?> </body> </html>
i'm aware session-chaos looks strange, read somewhere information isn't available across php-scripts, unless make session.
i'm grateful kind of - i've been fighting 48 hours
this basic approach, using easiest procedural php. first, load data in mysql php array follows, assuming you've made connections , id
id of specific row
$row = mysql_fetch_array(mysql_query("select * test `id` = '$id'"));
find way of ensuring that page displaying required id, instance, can use $id = $_get[row];
in case url http://url?row=id
in html code use echo 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 sessions, moment press submit should carry data in process.php
in global variable $_post or $_get, depending on method chose in <form method>
.
now in process.php, use
$nameid = mysql_real_escape_string($_post['nameid']); $id = $_post['id']; $sql = mysql_query("update test set `name_id` = '$nameid' `id` = '$id'"); if($sql) { echo "update successful"; }
needless say, need use database connections in both pages.
Comments
Post a Comment