Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: PHP Trouble

  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Trouble

    Ok, I have this code, to edit news:
    PHP Code:
    <?php
        $PageTitle
    ="Skaidon Designs - Admin Panel.";
    ?>
    <?php 
    include ('header.php'); ?>

    <?

    // form not yet submitted
    // display initial form with values pre-filled
    if (!$submit)
    {
        
    // open database connection
        
    $connection mysql_connect('localhost','****','*****') or die ("Unable to connect!");

        
    // select database
        
    mysql_select_db(testnews) or die ("Unable to select database!");

        
    // generate and execute query
        
    $query "SELECT title, newstext FROM news WHERE id = '$id'";
        
    $result mysql_query($query) or die ("Error in query: $query. " .
    mysql_error());

        
    // if a result is returned
        
    if (mysql_num_rows($result) > 0)
        {
            
    // turn it into an object
            
    $row mysql_fetch_object($result);

            
    // print form with values pre-filled
    ?>
    <form action="<? echo $PHP_SELF?>" method="POST">
    <table width="96%" align="center" cellpadding="0" cellspacing="1" class="contenttable">
        <tr>
            <th class="contentth" align="left" height="21" valign="middle">
            Title
            </th>
        </tr>
        <tr>
            <td class="contenttd" align="left" valign="top">
            <input size="50" maxlength="250" type="text" name="title" value="<? echo $row->title?>">
            </td>
        </tr>
        </tr>
        <tr>
            <th class="contentth" align="left" valign="middle">
            Description
            </th>
        </tr>
        <tr>
            <td class="contenttd" align="left" valign="top">
            <input name="newstext" cols="40" rows="10" value="<? echo $row->newstext?>" />
            </td>
        </tr>
        <tr>
            <th class="contentth" align="left" valign="middle">
            <input type="Submit" name="submit" value="Update">
            </th>
        </tr>
    </form>
    </table>
    <?
        
    }
        
    // no result returned
        // print graceful error message
        
    else
        {
            echo 
    "<span class='info'>That news article could not be located in our
    database.</span>"
    ;
        }
    }
    else
    {
    // form submitted
    // start processing it
        // set up error list array
        
    $errorList = array();
        
    $count 0;
        
        
    // validate text input fields
        
    if (!$title) { $errorList[$count] = "Invalid entry: Title"$count++; }
        
        if (!
    $newstext) { $errorList[$count] = "Invalid entry: News Text"$count++; }
        
        
    // check for errors
        // if none found...
        
    if (sizeof($errorList) == 0)
        {
            
    // open database connection
            
    $connection mysql_connect(localhost, ****, *****) or die ("Unable to connect!");

            
    // select database
            
    mysql_select_db(testnews) or die ("Unable to select database!");

            
    // generate and execute query
            
    $query "UPDATE news SET title = '$title', newstext = '$newstext' NOW() WHERE id = '$id'";
            
    $result mysql_query($query) or die ("Error in query: $query. " mysql_error());

            
    // print result
            
    echo "<span class='info'>Update successful. <a href=list.php>Go back to the main menu</a>.</span>";

            
    // close database connection
            
    mysql_close($connection);
        }
        else
        {
            
    // errors occurred
            // print as list
            
    echo "<span class='error'>The following errors were encountered: <br>";
            echo 
    "<ul>";
            for (
    $x=0$x<sizeof($errorList); $x++)
            {
                echo 
    "<li>$errorList[$x]";
            }
            echo 
    "</ul></span>";
        }
    }
    ?>

    <?php include ('footer.php'); ?>
    And I can't get this to work, instead of showing the news article for editing, it just shows the error text "That news article could not be located in our database."

    Any help would be greatly appreciated, Im very new to PHP, so this may be solved with something very simple.

  • #2
    New Coder
    Join Date
    Dec 2003
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    its because you didn't submit and you have no value for ' $id ' it goes to the else statement after the HTML part.

    i see no reason of that suff under if (!$submit).

    i would suggest using Smarty Template Engine, its very easy and worth it...you will have clean php code and not just that you can also skin the script. Sure it takes little bit of time to learn it but really its worth it.

    hope this helps,
    fimi

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't see $id being set anywhere
    I also don't see you specifying the collection ($_POST, $_SERVER etc.) so i assume you have regiter_globals = on

    I recommens turning them to off
    http://www.php.net/register_globals

    And then try changing
    <? echo $PHP_SELF; ?>"

    into

    <?php echo $_SERVER['PHP_SELF']; ?>"

    (don't rely on shorttags)


    To make sure $id has a value when the query is ran, change your code to
    $query = "SELECT title, newstext FROM news WHERE id = '$id'";
    echo $query;
    die();
    $result = mysql_query($query) or die ("Error in query: $query. " .

    and then load the page in your browser and look at the printed query
    <edit>Posts crossed</edit>

  • #4
    New Coder
    Join Date
    Dec 2003
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by raf

    To make sure $id has a value when the query is ran, change your code to
    $query = "SELECT title, newstext FROM news WHERE id = '$id'";
    echo $query;
    die();
    $result = mysql_query($query) or die ("Error in query: $query. " .

    and then load the page in your browser and look at the printed query
    Very useful thing to do... i use it very often when i see that there is something wrong getting the data from the db.

    tip: can laso use exit; -> is same as die();

  • #5
    New Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help thus far.

    I have ammended it, as you suggested, and you are right, the id isn't being passed over.

    I have one page, that shows the news and has a link to edit, it should pass over the id number (editnews.php?id=1) is this not the correct way to do this?

    Again I apologise for my "newbism", if you can point me to good articles on this, I would be greatful.

  • #6
    New Coder
    Join Date
    Dec 2003
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes you can do .....php?id=1

    here's a good tutorial simillar to what you are doing:
    click here

    and also search on google and stuff like that.


    WOW Hold on....

    if you are passing the id value by a link
    Why do you need the (!$submit) part in your code?
    you need to work that out.

    here are some tutorials
    http://www.hotscripts.com/PHP/Tips_a...ing/index.html
    Last edited by fimi; 12-20-2003 at 04:01 PM.

  • #7
    New Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, I have just realised that now (I was adapting some of my old code, years old )
    ight the code I have now is:
    PHP Code:
    <?php
        $PageTitle
    ="Skaidon Designs - Admin Panel.";
    ?>
    <?php 
    include ('header.php'); ?>
    <?
        
    // open database connection
        
    $connection mysql_connect('localhost','****','*****') or die ("Unable to connect!");

        
    // select database
        
    mysql_select_db(testnews) or die ("Unable to select database!");

        
    // generate and execute query
        
    $query "SELECT title, newstext FROM news WHERE id = '$id'";
        echo 
    $query;
        die();
        
    $result mysql_query($query) or die ("Error in query: $query. " .
        
    mysql_error());

        
    // if a result is returned
        
    if (mysql_num_rows($result) > 0)
        {
            
    // turn it into an object
            
    $row mysql_fetch_object($result);

            
    // print form with values pre-filled
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <table width="96%" align="center" cellpadding="0" cellspacing="1" class="contenttable">
        <tr>
            <th class="contentth" align="left" height="21" valign="middle">
            Title
            </th>
        </tr>
        <tr>
            <td class="contenttd" align="left" valign="top">
            <input size="50" maxlength="250" type="text" name="title" value="<? echo $row->title?>">
            </td>
        </tr>
        </tr>
        <tr>
            <th class="contentth" align="left" valign="middle">
            Description
            </th>
        </tr>
        <tr>
            <td class="contenttd" align="left" valign="top">
            <input name="newstext" cols="40" rows="10" value="<? echo $row->newstext?>" />
            </td>
        </tr>
        <tr>
            <th class="contentth" align="left" valign="middle">
            <input type="Submit" name="submit" value="Update">
            </th>
        </tr>
    </form>
    </table>
    <?
    // form submitted
    // start processing it
        // set up error list array
        
    $errorList = array();
        
    $count 0;
        
        
    // validate text input fields
        
    if (!$title) { $errorList[$count] = "Invalid entry: Title"$count++; }
        
        if (!
    $newstext) { $errorList[$count] = "Invalid entry: News Text"$count++; }
        
        
    // check for errors
        // if none found...
        
    if (sizeof($errorList) == 0)
        {
            
    // open database connection
            
    $connection mysql_connect(localhost, ****, *****) or die ("Unable to connect!");

            
    // select database
            
    mysql_select_db(testnews) or die ("Unable to select database!");

            
    // generate and execute query
            
    $query "UPDATE news SET title = '$title', newstext = '$newstext' NOW() WHERE id = '$id'";
            
    $result mysql_query($query) or die ("Error in query: $query. " mysql_error());

            
    // print result
            
    echo "<span class='info'>Update successful. <a href=list.php>Go back to the main menu</a>.</span>";

            
    // close database connection
            
    mysql_close($connection);
        }
        else
        {
            
    // errors occurred
            // print as list
            
    echo "<span class='error'>The following errors were encountered: <br>";
            echo 
    "<ul>";
            for (
    $x=0$x<sizeof($errorList); $x++)
            {
                echo 
    "<li>$errorList[$x]";
            }
            echo 
    "</ul></span>";
        }

    ?>
    <?php 
    include ('footer.php'); ?>
    And I get this error:
    Parse error: parse error, unexpected $ in C:\FoxServ\www\folio-\admin\editnews.php on line 103
    Can't seem to figure it out.

    Thanks for the help.

  • #8
    New Coder
    Join Date
    Dec 2003
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if the query works now you should delete the two lines you added

    echo $query;

    die();
    .....
    as for the error you get now, i see no problem on that code and the error points to an html part...unless you have not posted the whole script.

  • #9
    New Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help, Im re-doing some of the code, hopefully I won't have any porblems, but if I do, Ill post

    Thanks Again Everyone.

  • #10
    New Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right, hit a new snag now, lol, Im useless.

    Anyways, here is my newish code:
    PHP Code:
    <?php
        $PageTitle
    ="Skaidon Designs - Admin Panel.";
    ?>
    <?php 
    include ('header.php'); ?>
    <?
        
    // open database connection
        
    $connection mysql_connect($host$user$pass) or die ("Unable to connect!");

        
    // select database
        
    mysql_select_db($datab) or die ("Unable to select database!");

        
    // generate and execute query
        
    $query "SELECT title, newstext FROM news WHERE id = '$id'";
        
    $result mysql_query($query) or die ("Error in query: $query. " .
        
    mysql_error());

        
    // if a result is returned
        
    if (mysql_num_rows($result) > 0)
        {
            
    // turn it into an object
            
    $row mysql_fetch_object($result);
        }

            
    // print form with values pre-filled
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table width="96%" align="center" cellpadding="0" cellspacing="1" class="contenttable">
        <tr>
            <th class="contentth" align="left" height="21" valign="middle">
            Title
            </th>
        </tr>
        <tr>
            <td class="contenttd" align="left" valign="top">
            <input size="50" maxlength="250" type="text" name="title" value="<? echo $row->title?>">
            </td>
        </tr>
        </tr>
        <tr>
            <th class="contentth" align="left" valign="middle">
            Description
            </th>
        </tr>
        <tr>
            <td class="contenttd" align="left" valign="top">
            <textarea name="newstext" cols="40" rows="10"><? echo $row->newstext?></textarea>
            </td>
        </tr>
        <tr>
            <th class="contentth" align="left" valign="middle">
            <input type="Submit" name="submit" value="Update">
            </th>
        </tr>
    </form>
    </table>
    <?
    // form submitted
    // start processing it
        // set up error list array
    if ($submit)
    {
            
    // open database connection
            
    $connection mysql_connect($host$user$pass) or die ("Unable to connect!");

            
    // select database
            
    mysql_select_db($datab) or die ("Unable to select database!");

            
    // generate and execute query
            
    $query "UPDATE news SET title = '$title', newstext = '$newstext' NOW() WHERE id = '$id'";
            
    $result mysql_query($query) or die ("Error in query: $query. " mysql_error());

            
    // print result
            
    echo "<span class='info'>Update successful. <a href=index.php>Go back to the main menu</a>.</span>";

            
    // close database connection
            
    mysql_close($connection);
    }
    ?>
    <?php 
    include ('footer.php'); ?>
    Right, id is sent now, so that the fields are filled with the text, but, when edited and submitted, the id is no longer sent to the next part of the script, how do I go about doing this?

    Thanks

  • #11
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    that's as you're not passing the ID in your form. Change
    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    To
    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF'].'/'.$_SERVER['QUERY_STRING']; ?>" method="post">

  • #12
    New Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Now, this has the same error, but, it changes the link:

    editnews.php?id=1

    -- click update --

    editnews.php/id=1

    This now gives the same error.

    Any other ideas?

    Thanks

  • #13
    New Coder
    Join Date
    Dec 2003
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Nightfire
    To
    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF'].'/'.$_SERVER['QUERY_STRING']; ?>" method="post">
    [/B]

    change that to:

    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; ?>" method="post">
    he didn't hold shift when pressed the ? key on keyboard.

  • #14
    New Coder
    Join Date
    Dec 2003
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    lol

    well, now I get a weird error, but I think we are almost there now
    Error in query: UPDATE news SET title = 'Testing the Works of monkeys', newstext = 'Well, lets hope this works, otherwise I might just cry, yes, cry. Ho Hum' NOW() WHERE id = '1'. You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOW() WHERE id = '1'' at line 1
    Thats the error I get now.

    Any Ideas?

    Thanks.

  • #15
    New Coder
    Join Date
    Dec 2003
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    update query is wrong...
    tell me the filelds you are trying to update.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •