Page 1 of 1

Help for a PHP new commer

Posted: Tue Nov 24, 2009 2:07 pm
by Squidge
hello all.

I have been banging my head against a hard wall. I am new to PHP, and would like to use it so I can make my site dynamic.

I have some code which I would like some help on.

I have been told it is messy but I am new and was following a tutorial.

Basically, I am trying to use TinyMCE to edit, save, and update data on MySQL database

Code: Select all

 
<form action="<?=$_SERVER['PHP_SELF']?>" method="post"> 
<!--<form method="post" method="post">-->
<textarea name="TestText" cols="50" rows="15">
<?
//if (isset($_GET['addtest'])): // If the user wants to add a joke
//else: // Default page display
//Connect to database server
 
$dbcnx = @mysql_connect('localhost', 'USERNAME', 'PASSWORD');
if (!$dbcnx) {
  die( '<p>Unable to connect to the ' .
        'database server at this time.</p>
        <p>Please try again later</p>
        <p>If this continues to happen, please contact your system administrator</p>' );
}
 
    // Select the jokes database
    if (! @mysql_select_db('yellowz1_rtb') ) {
      die( '<p>Unable to locate the Round The Bend ' .
           'database at this time.</p>' );
    }
 
    // If a joke has been submitted,
    // add it to the database.
    if (isset($_POST['submittest'])) {
      $testtext = $_POST['testtext'];
      $sql = "INSERT INTO Testimonials SET
              TestText='$testtext'
              ";
      if (@mysql_query($sql)) {
        echo('<p>Your Testimonial has been added.</p>'); 
      } else {
        echo('<p>Error adding submitted joke: ' .
             mysql_error() . '</p>');
      }
    }
 
  /*  echo('<p> Here are all the Testimonials in our database: </p>');
 
    // Request the text of all the jokes
    $result = @mysql_query('SELECT TestText FROM Testimonials');
    if (!$result) {
      die('<p>Error performing query: ' .
          mysql_error() . '</p>');
    }
 
    // Display the text of each joke in a paragraph
    while ( $row = mysql_fetch_array($result) ) {
      echo('<p>' . $row['TestText'] . '</p>');
    }
 
    // When clicked, this link will load this page
    // with the joke submission form displayed.
    echo('<p><a href="' . $_SERVER['PHP_SELF'] .
         '?addtest=1">Add a Testimonial!</a></p>');
 
  endif;*/
 
?>
</textarea>
<input type="submit" name="submitjoke" value="SUBMIT" />  
</form>
Could someone take pitty on a new chap and help me out?

Basically I would like to tidy it up, and solve the issue I have, which is it doesn't save back to my database.

Now because it is whilst i was following a tutorial the comments are slightly out.

DB name is yellowz1_rtb, table it is linked to is TestText.

Please help

Re: Help for a PHP new commer

Posted: Tue Nov 24, 2009 2:54 pm
by tr0gd0rr
Perhaps it is because you are checking for $_POST['submittest'] but the name of the submit button is "submitjoke". A basic way to debug is to put error_log($var) or echo $var at different points to see what a particular variable equals.

If you get more fancy, you can use a debugger like xdebug or phped to step through the code to see every variable as the code executes.

Re: Help for a PHP new commer

Posted: Tue Nov 24, 2009 3:23 pm
by Squidge
tr0gd0rr wrote:Perhaps it is because you are checking for $_POST['submittest'] but the name of the submit button is "submitjoke". A basic way to debug is to put error_log($var) or echo $var at different points to see what a particular variable equals.

If you get more fancy, you can use a debugger like xdebug or phped to step through the code to see every variable as the code executes.
Thank you, I completley missed that :oops:

will have a look at the echo $var and xdebug

thank you

Re: Help for a PHP new commer

Posted: Wed Nov 25, 2009 6:50 am
by Squidge
1 quick question

How can I, once the Submit button has been used, redirect to a page??

Re: Help for a PHP new commer

Posted: Wed Nov 25, 2009 8:16 am
by mupparion
change form from

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

to

<form action="anything.php" method="post">

Ofc, be sure your php is sat on anything.php

or on line 59, after your php has done its stuff

header("Location: pagename.php");

Re: Help for a PHP new commer

Posted: Wed Nov 25, 2009 1:14 pm
by Squidge
mupparion wrote:change form from

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

to

<form action="anything.php" method="post">

Ofc, be sure your php is sat on anything.php

or on line 59, after your php has done its stuff

header("Location: pagename.php");
i tried the

Code: Select all

header("Location: apage.php");
but get the following error:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/yellowz1/public_html/url.info/admin/Testimonial.php:7) in /home/yellowz1/public_html/url.info/admin/Testimonial.php on line 98
Line 98 is the

Code: Select all

header("Location: apage.php");
This is a form inside a HTML page.

I also tried changing the

Code: Select all

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
But this stops the submittion of the data to my database.

I am being really stupid???

Re: Help for a PHP new commer

Posted: Wed Nov 25, 2009 2:31 pm
by superdezign
Headers can only be sent before any output is made to the screen. Typically, form processing is done before any output is ever made, as it is purely back-end. If you were to do this, then you could make use of header redirection after the form submission is complete.

Re: Help for a PHP new commer

Posted: Fri Nov 27, 2009 4:02 pm
by Squidge
ok i am stuck again :(

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
HTML CODING
 
<?
  if (isset($_GET['addtest'])): // If the user wants to add a joke
?>
 
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>Type your Testimonial here:<br />
<textarea name="testtext" rows="10" cols="40" wrap>
</textarea><br />
<input type="submit" name="submittest" value="SUBMIT" />
</p>
</form>
 
<?
  else: // Default page display
 
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
 
    // If a Testimonial has been submitted,
    // add it to the database.
    if (isset($_POST['submittest'])) {
      $testtext = $_POST['testtext'];
      $sql = "INSERT INTO Testimonials SET
              TestText='$testtext'
              ";
      if (@mysql_query($sql)) {
        echo('<p>Your Testimonial has been added.</p>'); 
      } else {
        echo('<p>Error adding Testimonial: ' .
             mysql_error() . '</p>');
      }
    }
    
    // If a Testimonial has been deleted,
    // remove it from the database.
    if (isset($_GET['deletetest'])) {
      $testid = $_GET['deletetest'];
      $sql = "DELETE FROM Testimonials
              WHERE ID=$testid";
      if (@mysql_query($sql)) {
        echo('<p>The Testimonial has been deleted.</p>');
      } else {
        echo('<p>Error deleting Testimonial: ' .
             mysql_error() . '</p>');
      }
    }
    // When clicked, this link will load this page
    // with the Testimonial submission form displayed.
    echo('<p><a href="' . $_SERVER['PHP_SELF'] .
         '?addtest=1">Add a Testimonial!</a></p>');
    
    echo('<p> Here are all the Testimonials in our database: </p>');
 
 
 
?>
</h2>
 
<?
    // Request the ID and text of all the Testimonials
    $result = @mysql_query('SELECT ID, TestText FROM Testimonials');
    if (!$result) {
      die('<p>Error performing query: ' .
          mysql_error() . '</p>');
    }
 
    // Display the text of each Testimonial in a paragraph
    // with a "Delete" link next to each.
    while ( $row = mysql_fetch_array($result) ) {
      $testid = $row['ID'];
      $testtext = $row['TestText'];
      echo('<p>' . $testtext .
           '<code><a href="' . $_SERVER['PHP_SELF'] .
           '?deletetest=' . $testid . '">' .
           'Delete</a></code></p>');
    }
 
  endif;
?>
 
HTML CODING
 
</html>
How do i edit and update my info in the table??

I just cant seem to figure it out..... :banghead:

Re: Help for a PHP new commer

Posted: Sat Nov 28, 2009 8:45 am
by Squidge
Can anyone help :(

Re: Help for a PHP new commer

Posted: Mon Nov 30, 2009 10:04 am
by superdezign
If you want help, describe the problem. We're all real people with real lives who help you with no compensation for doing so. If your question is too vague or requires too much work, we're more likely to ignore it.

Re: Help for a PHP new commer

Posted: Mon Nov 30, 2009 2:43 pm
by Squidge
superdezign wrote:If you want help, describe the problem. We're all real people with real lives who help you with no compensation for doing so. If your question is too vague or requires too much work, we're more likely to ignore it.
And that I understand.

With the code above, I am strugglerling to work out how to edit.

Basically what I have so far is this :

I am able to update, and delete from the DB. However I cannot get my head around how to edit what is already there.

I want to be able to update the content of the table but I am new and cannot work how it is done.

I am after guidence and help :oops:

Re: Help for a PHP new commer

Posted: Tue Dec 01, 2009 2:28 pm
by Squidge
still struggling....

:banghead: