Jump to content

What am I overlooking?


Cell0518

Recommended Posts

Hi, I've been using PHP / MySQL for a couple of months, off and on, to update my news section on my site. I am trying to change the "primary key", to move a story up to the top. It pulls from the database, the story_number (news story number). It pastes 2 times, one to a disabled input box, and 1 to an "update" input box that I can edit. It keeps telling me "can't add to table" , which means the query is not good. I tried putting the query, with data, in PHPMyAdmin and it works fine. I can't figure out what is wrong.

Thanks,
Chris

Clip from 1 php file

Sorry this is SO BIG... :(

EDIT: Should this be in the MySQL forum??

[code]
//EDIT

elseif ($action == "edit"){

require "../config.php";

$search_story_number = $_POST['story_number'];

$conn = mysql_connect("$location","$username","$password");

if (!$conn) die ("Could not connect MySQL");

mysql_select_db($database,$conn) or die ("Could not open database");

//Query for Story

$query  = "SELECT * FROM dslr_news WHERE story_number=$search_story_number LIMIT 1";

$result = mysql_query($query);

//Display Story

echo "<body style=\"text-align: center\">

<table width=\"800\" height=\"600\" align=\"center\" border=\"0\" id=\"standard-inner\" class=\"BLANK\">\n";

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

stripslashes($row['short_news']);

stripslashes($row['main_news']);

echo "<form method=\"post\" action=\"./?action=update\">Edit News<br />";

echo "Original Story Number<br /><input type=\"text\" name=\"orig_story_number\" id=\"orig_story_number\" value=\"";

echo $row['story_number'];

echo "\" disabled><br />\n";

echo "New Story Number<br /><input type=\"text\" name=\"new_story_number\" id=\"new_story_number\" value=\"";

echo $row['story_number'];

echo "\"><br />\n";

echo "News Title<br /><input type=\"text\" name=\"news_title\" id=\"news_title\" value=\"";

echo $row['news_title'];

echo "\"><br />\n";

echo "Short Title<br /><input type=\"text\" name=\"news_title_short\" id=\"news_title_short\" value=\"";

echo $row['news_title_short'];

echo "\"><br />\n";

echo "Date<br /><input type=\"text\" name=\"news_date\" id=\"news_date\" value=\"";

echo $row['news_date'];

echo "\"><br />\n";

echo "Short Date<br /><input type=\"text\" name=\"news_date_short\" id=\"news_date_short\" value=\"";

echo $row['news_date_short'];

echo "\"><br />\n";

echo "Short Excerpt<br /><textarea name=\"short_news\" id=\"short_news\" cols=\"55\" rows=\"5\">";

echo $row['short_news'];

echo "</textarea><br />";

echo "Main News<br /><textarea name=\"main_news\" id=\"main_news\" cols=\"55\" rows=\"10\">";

echo $row['main_news'];

echo "</textarea><br />";

echo "<input type=\"submit\" value=\"Edit News\">

<p />\n";

}

echo "</table>";

footer_main();

}

//ALTER

elseif ($action == "update"){

require "../config.php";

$conn = mysql_connect("$location","$username","$password");if (!$conn) die ("Could not connect MySQL");mysql_select_db($database,$conn) or die ("Could not open database");

$orig_story_number = $_POST['orig_story_number'];
$new_story_number = $_POST['new_story_number'];

$news_title = $_POST['news_title'];

$news_title_short = $_POST['news_title_short'];

$news_date = $_POST['news_date'];

$news_date_short = $_POST['news_date_short'];

$short_news = $_POST['short_news'];

$main_news = $_POST['main_news'];
$query = "UPDATE `dslr_news` SET `story_number` = '$new_story_number', `news_title` = '$news_title', `news_title_short` = '$news_title_short', `news_date` = '$news_date', `news_date_short` = '$news_date_short', `short_news` = '$short_news', `main_news` = '$main_news' WHERE `story_number` = $orig_story_number LIMIT 1";

    mysql_query($query) or die ("Could not add data to the table");

echo "<body style=\"text-align: center\">

Action Completed

<p /><a href=\"./?action=choose-edit\">Edit More News</a>";[/code]
Link to comment
Share on other sites

If your story number field is a primary key (and set to auto increment?) there's a high chance that when you're trying to change it mysql won't let you either because

a) there is already a row with the same story number
b) the field is set as auto increment at which point you should not need to manually enter a number

Have you thought about adding a date_story_posted (or similar named) field? This would enable you to use ORDER BY date_story_posted in your SQL statements and order them in either Ascending or descending order...?
Link to comment
Share on other sites

[!--quoteo(post=353552:date=Mar 10 2006, 06:17 PM:name=lessthanthree)--][div class=\'quotetop\']QUOTE(lessthanthree @ Mar 10 2006, 06:17 PM) [snapback]353552[/snapback][/div][div class=\'quotemain\'][!--quotec--]
If your story number field is a primary key (and set to auto increment?) there's a high chance that when you're trying to change it mysql won't let you either because
b) the field is set as auto increment at which point you should not need to manually enter a number
[/quote]

This is the case. I just wanted to be able to change them, if the need arose. If this isn't possible, I understand.

Can someone clarify this?
Link to comment
Share on other sites

[!--quoteo(post=353563:date=Mar 10 2006, 09:51 AM:name=Cell0518)--][div class=\'quotetop\']QUOTE(Cell0518 @ Mar 10 2006, 09:51 AM) [snapback]353563[/snapback][/div][div class=\'quotemain\'][!--quotec--]
This is the case. I just wanted to be able to change them, if the need arose. If this isn't possible, I understand.

Can someone clarify this?
[/quote]


You CAN manually enter data into an auto_increment field but the data MUST be unique.
Link to comment
Share on other sites

[!--quoteo(post=353564:date=Mar 10 2006, 06:57 PM:name=lessthanthree)--][div class=\'quotetop\']QUOTE(lessthanthree @ Mar 10 2006, 06:57 PM) [snapback]353564[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You CAN enter manually enter data into an auto_increment field but the data MUST be unique.
[/quote]
Well, with the code above, I did try and it seemed to fail. Do you see anything that would cause it to fail?
Link to comment
Share on other sites

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.