Jump to content

update mysql database script


webmazter

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/139643-update-mysql-database-script/
Share on other sites

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>

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>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.