webmazter Posted January 6, 2009 Share Posted January 6, 2009 I am having troubles with this code If i use the 2nd line i can see the table entries in the fields of the submit form, however i cannot update the database that way. if i hard code the id as in line 4 i cannot see the table entries but i can update the database. I would like to be able to use both see the fields and update the database. Any guru help here? <?php if(isset($_POST['Submit'])){ This Line HERE#$id=$r["id"];//take out the id $id = "1"; $name1 = $_REQUEST['name1']; $photo1 = $_REQUEST['photo1']; $sql = "UPDATE test SET name1 = '" . $name1 . "', photo1 = '" . $photo1 . "' WHERE id = '" . $id . "'"; $query = mysql_query($sql); if ($query) { header("location:1.php"); exit; } } $result=mysql_query("select * from test where id='$id'"); $row=mysql_fetch_assoc($result); ?> <!-- set this form to POST method and target itself ($PHP_SELF--> <style type="text/css"> <!-- .style1 { color: #000000; font-size: 14px; } --> </style> <body> <form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>"> <p class="style1">Name1 : <!-- name of this text field is "name1" --> <input name="name1" type="text" id="name1" value="<?php echo $row["name1"] ?>" size="20"/> <br /> photo1 : <!-- name of this text field is "photo1" --> <input name="photo1" type="text" id="photo1" value="<? echo $row['photo1']; ?>" size="20"/> </p> <p class="style1"><?php echo $row['name1']; ?></p> <p> <span class="style1"> <input type="submit" name="Submit" value="Submit" /> </span> </p> </form> </body> Quote Link to comment https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/ Share on other sites More sharing options...
PravinS Posted January 6, 2009 Share Posted January 6, 2009 You have redirected the page, so it is not showing the field values. Comment this line //header("location:1.php"); Quote Link to comment https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/#findComment-730680 Share on other sites More sharing options...
webmazter Posted January 6, 2009 Author Share Posted January 6, 2009 I have removed that part but nothing is updated on mysql. <?php if(isset($_POST['Submit'])){ $id=$r["id"];//take out the id $name1 = $_REQUEST['name1']; $photo1 = $_REQUEST['photo1']; $sql = "UPDATE test SET name1 = '" . $name1 . "', photo1 = '" . $photo1 . "' WHERE id = '" . $id . "'"; $query = mysql_query($sql); } $result=mysql_query("select * from test where id='$id'"); $row=mysql_fetch_assoc($result); ?> <!-- set this form to POST method and target itself ($PHP_SELF--> <style type="text/css"> <!-- .style1 { color: #000000; font-size: 14px; } --> </style> <body> <form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>"> <p class="style1">Name1 : <!-- name of this text field is "name1" --> <input name="name1" type="text" id="name1" value="<?php echo $row["name1"] ?>" size="20"/> <br /> photo1 : <!-- name of this text field is "photo1" --> <input name="photo1" type="text" id="photo1" value="<? echo $row['photo1']; ?>" size="20"/> </p> <p class="style1"><?php echo $row['name1']; ?></p> <p> <span class="style1"> <input type="submit" name="Submit" value="Submit" /> </span> </p> </form> </body> Quote Link to comment https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/#findComment-730952 Share on other sites More sharing options...
bluesoul Posted January 6, 2009 Share Posted January 6, 2009 Please (please) append your mysql_query()'s with or die(mysql_error()); and see what turns up. Quote Link to comment https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/#findComment-730956 Share on other sites More sharing options...
webmazter Posted January 6, 2009 Author Share Posted January 6, 2009 like this?>? $query = mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/#findComment-730969 Share on other sites More sharing options...
bluesoul Posted January 6, 2009 Share Posted January 6, 2009 like this?>? $query = mysql_query($sql) or die(mysql_error()); Yup. Quote Link to comment https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/#findComment-730972 Share on other sites More sharing options...
revraz Posted January 6, 2009 Share Posted January 6, 2009 Where do you get $r['id'] from to set $id? Echo $sql and see what it shows. Quote Link to comment https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/#findComment-730973 Share on other sites More sharing options...
webmazter Posted January 6, 2009 Author Share Posted January 6, 2009 Done, but no errors and not working still. i can view the data still it does not actully update. when i click submit from the url /2.php?id=2 i get returned now to /2.php <?php if(isset($_POST['Submit'])){ $id=$r["id"]; #$id = "1"; $name1 = $_REQUEST['name1']; $photo1 = $_REQUEST['photo1']; $sql = "UPDATE test SET name1 = '" . $name1 . "', photo1 = '" . $photo1 . "' WHERE id = '" . $id . "'"; $query = mysql_query($sql) or die(mysql_error()); } $result=mysql_query("select * from test where id='$id'")or die(mysql_error()) ; $row=mysql_fetch_assoc($result)or die(mysql_error()) ; ?> <body> <form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>"> <p class="style1">Name1 : <!-- name of this text field is "name1" --> <input name="name1" type="text" id="name1" value="<?php echo $row["name1"] ?>" size="20"/> <br /> photo1 : <!-- name of this text field is "photo1" --> <input name="photo1" type="text" id="photo1" value="<? echo $row['photo1']; ?>" size="20"/> </p> <p class="style1"><?php echo $row['name1']; ?></p> <p> <span class="style1"> <input type="submit" name="Submit" value="Submit" /> </span> </p> </form> </body> Quote Link to comment https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/#findComment-730975 Share on other sites More sharing options...
webmazter Posted January 6, 2009 Author Share Posted January 6, 2009 Where do you get $r['id'] from to set $id? from the url 2.php?id=2 Echo $sql and see what it shows. i dont unsderstand Quote Link to comment https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/#findComment-730977 Share on other sites More sharing options...
revraz Posted January 6, 2009 Share Posted January 6, 2009 The URL is not setting $id because you are not using $_GET to retrieve it. $id=$_GET["id"]; Quote Link to comment https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/#findComment-730979 Share on other sites More sharing options...
l_kris06 Posted January 6, 2009 Share Posted January 6, 2009 Hope this helps, I havent tested, but it must work perfectly. <html> <head> <style type="text/css"> <!-- .style1 { color: #000000; font-size: 14px; } --> </style> </head> <body> <form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>"> <p class="style1">Name1 : <!-- name of this text field is "name1" --> <input name="name1" type="text" id="name1" value="<?php echo $row["name1"] ?>" size="20"/> <br /> photo1 : <!-- name of this text field is "photo1" --> <input name="photo1" type="text" id="photo1" value="<? echo $row['photo1']; ?>" size="20"/> </p> <p class="style1"><?php echo $row['name1']; ?></p> <p> <span class="style1"> <input type="submit" name="Submit" value="Submit" /> </span> </p> </form> <?php if(isset($_POST['Submit'])){ $id="100"; // Alter this later // $r["id"];//take out the id $name1 = "hello"; //$_REQUEST['name1']; $photo1 = "photo"; //lets check to whether update or insert $check = "select id from test where id='$id';"; $success = mysql_query($check); $ret = mysql_num_rows($success); if($ret == 0){ // We need to insert the values. $sql = "insert into test(id,name1,photo1)values('$id','$name1','$photo1');"; }elseif($ret==1){ //update , i assume id is a primary key $sql = "update test set name1='$name1',photo1='$photo1' where id='$id';"; } $query = mysql_query($sql); if($query){ //success indicator $result = mysql_query("select * from test where id='$id'"); $row = mysql_fetch_assoc($result); }else{ echo "Query failed."; exit; } } ?> </body> </html> Rgds, Kris Quote Link to comment https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/#findComment-731012 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.