Log in

View Full Version : Updating database table problem



shas1280
10-19-2011, 07:41 AM
I am trying to create a news manager with admin control to edit, delete, add functions, every thing is working fine instead of editing table data.

i need help from any experts.
here is my update.php and update_ac.php


update.php



<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="news_manager"; // 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="567" border="1" align="center" cellspacing="0">
<form enctype="multipart/form-data" action="update_ac.php" method="post">
<tr>
<td><strong>Edit</strong></td>
<td><input name="title" type="text" id="title" value="<? echo $rows['id']; ?>" size="10"></td>
</tr>
<tr>
<td width="89">Title</td>
<td><input name="title" type="text" id="title" value="<? echo $rows['title']; ?>" size="40"></td>
</tr>
<tr>
<td><strong>Sub Title</strong></td>
<td><input name="subtitle" type="text" id="subtitle" value="<? echo $rows['subtitle']; ?>" size="40"></td>
</tr>

<tr>
<td>News</td>
<td rowspan="2"><textarea name="news" cols="65" rows="9" id="news"><? echo $rows['news']; ?></textarea> </textarea></td>
</tr>




<tr>
<td>&nbsp;</td>
</tr>

<tr>
<td>Read More</td>
<td rowspan="2"><textarea name="fulltext" cols="65" rows="9" id="fulltext"><? echo $rows['fulltext']; ?></textarea> </textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td height="100">Image</td>
<td class="image_wrapper"><? echo "<img src=http://localhost:8888/doctorsByspeciality/images/".$rows['photo'] ."> <br>"; ?></td>
</tr>



<tr>
<td height="31">Change Photo</td>
<td><input type="file" name="photo"></td>
</tr>

<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
</form>
</table>





update_ac.php


<?php


$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="news_manager"; // 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");


//This is the directory where images will be saved
$target = "../images/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form

$title=$_POST['title'];
$subtitle=$_POST['subtitle'];
$news=$_POST['news'];
$photo=($_FILES['photo']['name']);
$fulltext=$_POST['fulltext'];




// update data in mysql database
$sql="UPDATE news_manager SET title='$title', subtitle='$subtitle', news='$news', photo='$photo', fulltext='$fulltext' WHERE id='$id'";



$result=mysql_query($sql);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
echo "Successful";

echo "<BR>";
echo "<a href='admin1.php'>Go Back To Admin Panel</a>";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";

}




?>







Thank you.

csscoder
11-01-2011, 03:34 AM
Hello,
Did you check the column lengh?