473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Update news entry

1
Hi, I was wondering if anyone could help me with this problem. Here is the case, I have a News database which you are able to view here:

http://lotrtw.com/news/index_.php

And users will be able to add/delete/edit. Ive worked out how to add and delete entries but Im stuck with the editing part, everytime when you click the Edit button after checking a box of the record you want to edit (and ofcourse changed the text inside the field) it gets the data from the last item you added instead of the text from the checked record. Can anyone help me so that it will take the information from the checked record.
Sorry if Im a bit vague, check out what happens here:

http://lotrtw.com/news/news_admin.php

News_del.php, where the delete and edit command get executed.

[PHP]<?php
$host="localhost";
$user="xxx";
$pass="xxx";
$db="news_";

$connect = mysql_connect($host, $user, $pass);
$dbselect = mysql_select_db($db, $connect);
$query = "SELECT * FROM news ORDER BY id";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);

if(isset($_POST["del"]))
{
for($i=0;$i<$num_rows;$i++)
{
$cb = $_POST['cb'];
$delete_records = $cb[$i];
$sql = "DELETE FROM news WHERE ID=$delete_records";
$result = mysql_query($sql);
}
echo '<script language="javascript">alert("Deletion succesful, returning to admin...")</script>';
echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=news_admin.php'> ";
}
elseif(isset($_POST["edit"]))
{
for($i=0;$i<$num_rows;$i++)
{
$cb = $_POST['cb'];
$edit_records = $cb[$i];
$title = $_POST['rows1'];
$sql = "UPDATE news SET title = '$title' WHERE id=$edit_records";
$result = mysql_query($sql);
}
echo '<script language="javascript">alert("Edit succesful, returning to admin...")</script>';
echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=news_admin.php'> ";
}
else
{
echo '<script language="javascript">alert("Edit/deletion failed, returning to admin...")</script>';
}

mysql_close();
?>
[/PHP]


News_admin.php, where you select the records and input new data

[PHP]<?php
//$user_chk=$_POST['user'];
//$pass_chk=$_POST['pass'];

$user_test="xxx";
$pass_test="xxx";

$host="localhost";
$user=$user_test;
$pass=$pass_test;
$db="news_";

$connect = mysql_connect($host, $user, $pass);
$dbselect = mysql_select_db($db, $connect);
$query = "SELECT * FROM news ORDER BY submitted";
$result = mysql_query($query);

$submit=$_POST['submit'];

//if(($user_chk == $user_test) & ($pass_chk == $pass_test))
//{
echo "<iframe src='news_add.php' height='217' width='100%' scrolling='no' name='frame'>
</iframe>";
echo "<form action='submit.php' method='post'>
<input type='submit' name='submit' value='Refresh content' style='outline:solid'/>
</form>";

echo "<form action='news_del.php' method='post'>";
echo ("<table border='1' width='100%' cellspacing='2' height='100'>");
echo "<tr border='0'><td colspan='6'><input type='submit' name='del' value='Delete selected records' style='outline:solid'/>' '<input type='submit' name='edit' value='Edit selected records' style='outline:solid'/></td></tr>";
while($rows = mysql_fetch_row($result))
{
echo ("<tr bgcolor='lightblue'><td><input type='checkbox' name='cb[]' value='$rows[0]'></td><td><input type='text' name='rows1' value='$rows[1]'></td><td><input type='text' name='rows2' value='$rows[2]' size='100'></td><td><input type='text' name='rows3' value='$rows[3]'></td><td><input type='text' name='rows4' value='$rows[4]'></td><td><input type='text' name='rows5' value='$rows[5]'></td></tr>");
}
echo "</form>";
echo "</table>";
/*}
else
{
echo "Error: Return to login screen <a href='news_login.php'>here</a>.";
}*/

?>[/PHP]
Dec 4 '07 #1
1 2550
stepterr
157 100+
Hi mac89,
I do something similar except I'm deleting a record from the database where the user has checked a box. I find doing a foreach is easier than a for loop for this kind of thing, at least that is my personal preference. So maybe seeing how I do it will help you.

This is within my form. I'm doing a while loop, like you are, to go through each record in the database.
[PHP]
echo '<td>Check to Delete this photo:';
echo '<input type="checkbox" name="delPhoto[]" value="' . $array['photoid'] . '" </td>';
[/PHP]

When the form is submitted:

[PHP]
if (!empty($_POST['delPhoto']))
{
foreach($_POST['delPhoto'] as $v)
{
$del_id = mysql_real_escape_string($v);
$sql = "DELETE FROM portfolio WHERE photoid ='$del_id'";
mysql_query($sql);
}
} [/PHP]
Jan 2 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Guinness Mann | last post by:
I have a column in one of my tables that needs to have about half of its values updated. The data looks like this: A\B0andSomeVaryingStuff.mp3 I'd like to update it to: ...
2
by: Richard Cornford | last post by:
Anyone who has taken a look at the online FAQ today may have noticed that I have updated it. The majority of the changes are the updating of broken links and the implementation of that extensive...
6
by: Jeff Kowalczyk | last post by:
I need to adapt this an update statement to a general form that will iterate over multiple orderids for a given customerinvoiceid. My first concern is a form that will work for a given orderid,...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
31
by: Lag | last post by:
Having a problem updating my database from a web page, through a submission form. Can anyone help? ----THIS IS MY CODE IN update.php----(user, pass, and database are typed in directly, I...
6
by: Dixie | last post by:
I have asked this question before, but I could not get the suggested solution work. So I will give more details this time. I have an append query that adds several hundred records to a table...
5
by: njb35 | last post by:
Hi all I'm beginning my foray from VBA into VB 2005 Express, and enjoying some of the efficiencies it provides! I'm stuck with some dataset handling however that I _think_ can be automated but...
5
by: P.J.M. Beker | last post by:
Hi there, I'm currently writing a program in which I use the FileMonitor to monitor a folder in which I store downloaded images. I know that I can't add much coding in the filemonitor's event in...
4
by: ahling | last post by:
Hi all. As mentioned in my first post, I'm currently doing an asp.net blog where user can update their profile, and update their blog entry. But I encountered problem with the update of entry...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.