MYSQL PROBLEM

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

MYSQL PROBLEM

Post by John Cartwright »

Code: Select all

<?php

  // Connect to the database server 
  $dbcnx = @mysql_connect("localhost", "********", 
"**********"); 
  if (!$dbcnx) { 
    echo( "<p>Unable to connect to the " . 
          "database server at this time.</p>" 
); 
    exit(); 
  } 
  // Select the newsdatabase 
  if (! @mysql_select_db("jcart_runtings") ) { 
    echo( "<p>Unable to locate the" . 
          "database at this time.</p>" ); 
    exit(); 
  } 

echo "<form action="=$PHP_SELF\ method="post">\n" 
."<p>Type your news here:<br/>\n" 
."<textarea name="news" rows="10" cols="40" wrap></textarea><br/>\n" 
."<input type="submit" name="submitnews" value="SUBMIT" /></p>\n" 
."</form>\n";


if ($submitnews == "SUBMIT") { 
  $sql = "INSERT INTO news SET 
          news='$news', 
          newsDate=CURDATE()"; 
  if (@mysql_query($sql)) { 
    echo("<p>Your News has been 
added.</p>"); 
  } else { 
    echo("<p>Error adding submitted news: " . 
         mysql_error() . "</p>"); 
  } 
} 

?>
?>

When I try using that form to add information I get brought to a link like this

http://www.******.com/tests/=/tests/index2.php/%20method=?news=THIS+IS+THE+NEWS&submitnews=SUBMIT

and the directory of the file is http://www.*****.com/tests/

BTW the data is not entered in the mysql
Last edited by John Cartwright on Sun Jan 25, 2004 10:21 am, edited 2 times in total.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Looks like you're missing a quotation mark from the end of your $PHP_SELF bit. By the way it's always better to use $_SERVER["PHP_SELF"].

echo "<form action=\"={$_SERVER['PHP_SELF']}\" <<<< LOOK
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

TY I'll be back soon :D TY once again





I love how this place has such quick responses :D
mad <3
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I added the missing quotation and now it's bringing me to this URL

http://www.********.com/tests/=/tests/index2.php
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

echo "<form action=\"={$_SERVER['PHP_SELF']}\"

remove the = ...

echo "<form action=\"{$_SERVER['PHP_SELF']}\"
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Okay how would I display some info in jcart_runtings ( table ) > news >

and then the fields, message, newsDate, etc

and how am I supposed to know which one is which,
i set ID to auto increment by 1... I think that was the right thing to do




Also, how am I supposed to display the newest-oldest news article

TY
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?
 
   // Connect to the database server 
  $dbcnx = @mysql_connect("*****", "******", 
"******"); 
  if (!$dbcnx) { 
    echo( "<p>Unable to connect to the " . 
          "database server at this time.</p>" 
); 
    exit(); 
  } 
  // Select the news database 
  if (! @mysql_select_db("jcart_runtings") ) { 
    echo( "<p>Unable to locate the " . 
          "database at this time.</p>" ); 
    exit(); 
  } 

$result = mysql_query("SELECT `newsDate` FROM `news` ORDER BY `id` DESC LIMIT 10") 




?>

Code: Select all

I finally got it right without getting any error messages but the field isn't showing up :S :S.... any thoughts?

Btw there is info in the mysql db to be displayed
Last edited by John Cartwright on Sun Jan 25, 2004 10:57 am, edited 4 times in total.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

An example might be $result = mysql_query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 10") and then use while($arr = mysql_fetch_array($result){} to loop throught the results.

Check these out for more info...
:: [php_man]mysql_query[/php_man]
:: [php_man]mysql_fetch_array[/php_man]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Read my last post I updated it ... TY :D


BTW looking in phpadmin what does null for each field mean?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

HURRAY I FIGURED IT OUT :D

ty
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

echo "<form action=\"{$_SERVER['PHP_SELF']}\"\n"
."<p>Type your news here:<br/>\n"
."<textarea name=\"title\" rows=\"1\" cols=\"40\" wrap></textarea><br/>\n"
."<textarea name=\"message\" rows=\"10\" cols=\"40\" wrap></textarea><br/>\n"
."<textarea name=\"signature\" rows=\"1\" cols=\"40\" wrap></textarea><br/>\n"
."<input type=\"submit\" name=\"submitnews\" value=\"SUBMIT\" /></p>\n"
."</form>\n";


if ($submitnews == "SUBMIT") {
$sql = "INSERT INTO news SET
message='$message',
newsDate=CURDATE(),
title='$title'
signature='$sig'";
if (@mysql_query($sql)) {
echo("<p>Your News has been added.</p>");
} else {
echo("<p>Error adding submitted news: " . mysql_error() . "</p>");
}
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

nvm i got it
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Holy Christ! 4 posts in a row?! Um..in the future, I would use that lil edit button over there to the right.
Post Reply