Jump to content

Recommended Posts

this is what i have so far but i cant get it to update the data

 

update page

 

<?php
$host="localhost"; // Host name
$username="**"; // Mysql username
$password="*****"; // Mysql password
$db_name="manews"; // Database name
$tbl_name="Shows"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];


// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Day</strong></td>
<td align="center"><strong>Month</strong></td>
<td align="center"><strong>Year</strong></td>
<td align="center"><strong>Song</strong></td>
<td align="center"><strong>Data</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="Day" type="text" id="Day" value="<? echo $rows['Day']; ?>"></td>
<td align="center"><input name="Month" type="text" id="Month" value="<? echo $rows['Month']; ?>"></td>
<td align="center"><input name="Year" type="text" id="Year" value="<? echo $rows['Year']; ?>" size="15"></td>
<td align="center"><input name="Song" type="text" id="Song" value="<? echo $rows['Song']; ?>"></td>
<td align="center"><input name="Data" type="text" id="Data" value="<? echo $rows['Data']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?

// close connection
mysql_close();

?>

 

update script

 

<?php
$host="localhost"; // Host name
$username="**"; // Mysql username
$password="**"; // Mysql password
$db_name="manews"; // Database name
$tbl_name="Shows"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database
$sql="UPDATE $tbl_name SET Day='$Day', Month='$Month', Year='$Year', Year='$Song', Year='$Data' WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/210311-mysql-and-php-update/
Share on other sites

here it is

 

<?php 
$host="localhost"; // Host name
$username="***"; // Mysql username
$password="****"; // Mysql password
$db_name="manews"; // Database name
$tbl_name="Shows"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>

<tr>
<td align="center"><strong>Day</strong></td>
<td align="center"><strong>Month</strong></td>
<td align="center"><strong>Year</strong></td>
<td align="center"><strong>Song</strong></td>
<td align="center"><strong>Data</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['Day']; ?></td>
<td><? echo $rows['Month']; ?></td>
<td><? echo $rows['Year']; ?></td>
<td><? echo $rows['Song']; ?></td>
<td><? echo $rows['Data']; ?></td>


<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>

try this

 

<?php
$link = mysql_connect("localhost","id","thepw");
mysql_select_db("dbname");

$query = "INSERT INTO db_name (field1, field2, field3, field4) VALUES('$data1','$data2','$data3','$data4')";

$result = mysql_query($query);
$sentOk = "The data has been added to the database.";
?>

// update data in mysql database
$sql="UPDATE $tbl_name SET Day='$Day', Month='$Month', Year='$Year', Year='$Song', Year='$Data' WHERE id='$id'";
$result=mysql_query($sql);

 

You are referencing variables that have not been defined in your script. Specifically $Day, $Month, $Year, $Song, $Data and $id.  It would appear that you expect these to be coming from the form.  However, the register_globals functionality of PHP has been turned off and will be removed from a soon-to-be-released version of PHP. It was a major security whole.

 

You need to retrieve these variables from the $_POST array before referencing them. You should also sanitize them to prevent SQL injections. And check that they are valid

 

$day = mysql_real_escape_string($_POST['day']);
// etc. etc., etc., 
// OR if they are integers, you can use
$day = intval($_POST['day']);
// which might end up as zero if it was not a valid number

 

Note: event the "id" value which you pass as a hidden field needs to be sanitized and validated.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.