PDA

View Full Version : MySQL Not Responding...


Existence
01-21-2005, 06:53 AM
Sorry to bother you people at CodingForums.com, but I'm having some sort of intricate problem with retrieving and inserting information into my database with my scripting.

I know, that my database works, because I run a Woltlab forum functionally and properly that retrieves and sends it's information and works totally fine.

But, here is my problem. I have a few scripts that are supposed to send queries to MySQL, and retrieve the data, and in one ( which is a news script ) that inserts data. But, when I push my submit buttons, nothing is inserted into the databasing. I'll show you the scripts that I'm using.

Script 1. Globals.php

<?php

$dbhost="localhost";

$dbusername="Censored";

$dbpassword="Censored";

$dbname="Existence";

$connect = mysql_connect($dbhost, $dbusername, $dbpassword);

mysql_select_db($dbname,$connect) or die ("Good job, idiot. You broke it.");

?>


Script 2. Add_News.php

<?php

include("Globals.php");



if($submit)

{//begin of if($submit).

// Set global variables to easier names

// and pervent sql injection and apostrophe to break the db.

$Title = mysql_real_escape_string($_POST['Title']);

$Text1 = mysql_real_escape_string($_POST['Text1']);

$Text2 = mysql_real_escape_string($_POST['Text2']);



//check if (title) field is empty then print error message.

if(!$Title){ //this means If the title is really empty.

echo "Error: Goddammit, put a title in the update..";

exit(); //exit the script and don't do anything else.

}// end of if



//run the query which adds the data gathered from the form into the database

$result = mysql_query("INSERT INTO news (Title, DTime, Text1, Text2)

VALUES ('$Title',NOW(),'$Text1','$Text2')",$connect);

//print success message.

echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";

echo "<meta http-equiv=Refresh content=4;url=News.php>";

}//end of if($submit).





// If the form has not been submitted, display it!

else

{//begin of else



?>

<br>

<h3>::Add News</h3>



<form method="post" action="<?php echo $PHP_SELF ?>">



Title: <input name="Title" type="text" size="40" maxlength="255">

<br>

Text1: <textarea name="Text1" rows="7" cols="30"></textarea>

<br>

Text2: <textarea name="Text2" rows="7" cols="30"></textarea>

<br>

<input type="submit" name="submit" value="Add News">

</form>

<?

}//end of else





?>


Script 3. News.php


<?php

// load the configuration file.

include("Globals.php");

//load all news from the database and then OREDER them by newsid

//you will notice that newlly added news will appeare first.

//also you can OREDER by (dtime) instaed of (news id)

$result = mysql_query("SELECT * FROM News ORDER BY NewsID DESC",$connect);

//lets make a loop and get all news from the database

while($myrow = mysql_fetch_assoc($result))

{//begin of loop

//now print the results:

echo "<b>Title: ";

echo $myrow['Title'];

echo "</b><br>On: <i>";

echo $myrow['DTime'];

echo "</i><hr align=left width=160>";

echo $myrow['Text1'];

// Now print the options to (Read,Edit & Delete the news)

echo "<br><a href=\"read_more.php?newsid=$myrow[newsid]\">Read More...</a>

|| <a href=\"edit_news.php?newsid=$myrow[newsid]\">Edit</a>

|| <a href=\"delete_news.php?newsid=$myrow[newsid]\">Delete</a><br><hr>";

}//end of loop

?>



---------
Please take a moment and look these over and tell me what is being done wrong...

You can E-mail me at ExistenceInfinito@Gmail.com , or leave a comment here -- either way, I'll check often. Thank you for taking the time.

-Jordan.

Brandoe85
01-21-2005, 07:08 AM
Welcome here! It's not bothering, we are here to help.
Ok, where is $submit defined? Try changing your if statement to:
if(isset($_POST['submit'])), instead of if($submit)

Existence
01-21-2005, 07:12 AM
Oh my God. Thank you, Brando. That's exactly what it needed.

I've only begun to start with PHP, and I found this script online. I hope that one day I'll be able to code this from scratch.

Thank you for your time, Brando -- you're awesome.

Brandoe85
01-21-2005, 07:15 AM
It's no problem at all. good luck with future coding! :thumbsup:

Existence
01-21-2005, 07:24 AM
Alright, now I have a second question -- Because this code ( from what I can read ) will display every archived file, I don't know how to only grab the top five rows from my DB. Is it like ( In the SQL of "SELECT * FROM news ORDER BY NewsID DESC" ) Do I change it from "SELECT * FROM news ORDER BY NewsID DESC FETCH TOP 5 ROWS " or something. I feel like an idiot. Haha.

If you could help me with this final thing, my script will be completed.

Brandoe85
01-21-2005, 07:36 AM
You could do, SELECT * FROM news ORDER BY NewsID DESC LIMIT 5.

Existence
01-21-2005, 07:44 AM
Haha, thanks again, padre. You're a Genius.

Brandoe85
01-21-2005, 07:46 AM
You're welcome! :thumbsup: