Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

update page

  • 19-11-2009 2:32pm
    #1
    Registered Users, Registered Users 2 Posts: 287 ✭✭


    very close to finishing my cms - got login to work trying to to the update script i can get it to pull the data in but not update

    here is the code:

    <?php
    // Connect to the database
    $cnx = mysql_connect("localhost", "*******", "*******")
    OR die("Unable to connect to database!");
    mysql_select_db("squarebar_db1", $cnx);

    if ($_POST == 1) {
    // Save to the database
    $data = mysql_real_escape_string(trim($_POST));
    $res = mysql_query("UPDATE news SET data = '".$data."' WHERE id = $id");

    if (!$res)
    die("Error saving the record! Mysql said: ".mysql_error());

    // Redirect to self to get rid of the POST
    header("Location: select.php");
    }

    include_once "fckeditor/fckeditor.php";
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml&quot; xml:lang="en" lang="en">
    <head>
    <title>Test FCKeditor</title>
    </head>
    <body>

    <h1><font size="2" face="Arial, Helvetica, sans-serif"> FCK Editor</font></h1>

    <form action="select.php" method="post">
    <?php
    // Get data from the database
    $query = mysql_query("SELECT data FROM news WHERE id = $id");
    $data = mysql_fetch_array($query);

    // Configure and output editor
    $oFCKeditor = new FCKeditor('fcktext');
    $oFCKeditor->BasePath = "fckeditor/";
    $oFCKeditor->Value = $data["data"];
    $oFCKeditor->Width = 540;
    $oFCKeditor->Height = 400;
    echo $oFCKeditor->CreateHtml();
    ?>
    <br />
    <input type="hidden" name="submit_form" value="1" />
    <input type="submit" value="Save Form" />
    </form>

    </body>
    </html>


    <?php
    // Close the database connection
    mysql_close($cnx);
    ?>


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    what is the result of testing the page? what do you mean by "i can get it to pull the data in but not update"?


  • Registered Users, Registered Users 2 Posts: 287 ✭✭Keewee6


    Adam wrote: »
    what is the result of testing the page? what do you mean by "i can get it to pull the data in but not update"?

    i can get the data to come into the form but when i change the text and hit save - it wont change


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    $res = mysql_query("UPDATE news SET data = '".$data."' WHERE id = $id");
    
    $id isn't set prior to this line

    EDIT : ACTUALLY, $id isn't set ANYWHERE in the above code, so I'm not even sure what the "SELECT" statement is selecting.


  • Registered Users, Registered Users 2 Posts: 287 ✭✭Keewee6


    ok thanks

    so would i set $id=id above the select query


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    well firstly, where are you getting the value for $id in this line? :

    [php]$query = mysql_query("SELECT data FROM news WHERE id = $id"); [/php]

    and secondly, if that's covered somehow, you need to send that value through the form so that the database query has an ID for the appropriate row to update in the table i.e. the value of $id in this line :

    [php]$res = mysql_query("UPDATE news SET data = '".$data."' WHERE id = $id"); [/php]

    so for example, add this to your form:

    [html]<input type="hidden" name="row_id" value="<?= $data; ?>" />[/html]

    and then use $_POST in place of $id in the update query.


  • Advertisement
  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Keewee6 wrote: »
    ok thanks

    so would i set $id=id above the select query

    Setting $id=id will mean nothing.

    What is id meant to represent ? Where and how does someone choose which record they want to update ?

    You'll need to pass the $id from that page using a $_GET or $_POST parameter, or a $_SESSION variable or something.

    The level of help that you need depends on how familar you are with PHP, MySQL & databases....normally (if someone were familiar and didn't ask the question above) they'd say "Aw, crap; forgot to set that - thanks!"

    The fact that you asked the above would imply that you're not familiar with them ? Am I right ?


  • Registered Users, Registered Users 2 Posts: 287 ✭✭Keewee6


    not sure actually where do i set this


  • Registered Users, Registered Users 2 Posts: 287 ✭✭Keewee6


    Liam Byrne wrote: »
    Setting $id=id will mean nothing.

    What is id meant to represent ? Where and how does someone choose which record they want to update ?

    You'll need to pass the $id from that page using a $_GET or $_POST parameter, or a $_SESSION variable or something.

    The level of help that you need depends on how familar you are with PHP, MySQL & databases....normally (if someone were familiar and didn't ask the question above) they'd say "Aw, crap; forgot to set that - thanks!"

    The fact that you asked the above would imply that you're not familiar with them ? Am I right ?

    correct but im workin on it


  • Closed Accounts Posts: 368 ✭✭DrivingInfo


    Hi Keewee

    If i remember correctly you need to put the output "id" of your select query in to a hidden field and the reference that hidden field when running the update query

    $query = mysql_query("SELECT data, id FROM news WHERE id = $id");

    This is what Adam is trying to help you with.


  • Registered Users, Registered Users 2 Posts: 287 ✭✭Keewee6


    Hi Keewee

    If i remember correctly you need to put the output "id" of your select query in to a hidden field and the reference that hidden field when running the update query

    $query = mysql_query("SELECT data, id FROM news WHERE id = $id");

    This is what Adam is trying to help you with.

    problem is im not sure how to do this


  • Advertisement
  • Closed Accounts Posts: 368 ✭✭DrivingInfo


    Keewee6 wrote: »
    problem is im not sure how to do this

    What field are in the news table in the database,

    id - data - something else - and so on - and so on

    am i right?


  • Registered Users, Registered Users 2 Posts: 287 ✭✭Keewee6


    What field are in the news table in the database,

    id - data - something else - and so on - and so on

    am i right?

    id
    date
    data

    are the fields


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Keewee6 wrote: »
    correct but im workin on it

    Well, essentially your page is pretty much right, in that it does the 2 things that you want:

    1) Show the editable record
    2) Update the editable record

    Unfortunately, both of those translate to "show this record" and "update this record", but you just haven't given it any indication as to what this record is.

    Firstly, rather than mysql_fetch_array, I'd use mysql_fetch_assoc, as it makes the code easier to understand, as you can reference record fields by name.

    If you were to have another page - "listing.php" that listed all current records, and each record had an "EDIT" link that called the page below (let's assume that it's called "update.php", then you could do the following

    1) On the listing.php page, the edit links would be
    <a href="update.php?id=<?php echo $data["id"]; ?>">EDIT</a>
    

    2) Within the update.php page, change the $_POST check to read
    if ($_POST['submit_form'] == 1) { 
    // Save to the database 
    $id=$_POST["id"];
    $data = mysql_real_escape_string(trim($_POST['fcktext'])); 
    $res = mysql_query("UPDATE news SET data = '".$data."' WHERE id = $id"); 
    
    if (!$res) 
    die("Error saving the record! Mysql said: ".mysql_error()); 
    
    // Redirect to self to get rid of the POST 
    header("Location: select.php"); 
    } else { 
    $id=$_GET["id"];
    }
    

    3) Put the ID in a hidden field so that it can be transferred from the edit page to the update
    <input type="submit" value="Save Form" /> 
    <input type="hidden" name="id" value="<?php echo $id; ?>" /> 
    


  • Closed Accounts Posts: 368 ✭✭DrivingInfo


    Keewee6 wrote: »
    id
    date
    data

    are the fields

    Ok in your select query put id and data.

    and then in the same way that you put the "data" information/output (from the database) into you update form put the "id" output into a hidden field and the reference that field when updating.


  • Registered Users, Registered Users 2 Posts: 287 ✭✭Keewee6


    getting VERY close just or two issues

    error
    Parse error: syntax error, unexpected '[' in /home/squarebar/domains/nixysthesquarebar.com/public_html/cmslogin/update.php on line 21

    code
    <?php
    include_once "fckeditor/fckeditor.php";
    $cnx = mysql_connect("localhost", "**********", ""**********", ")
    OR die("Unable to connect to database!");
    mysql_select_db("*****", $cnx);

    $query = "SELECT * FROM news WHERE id= $_GET[id]";
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="XHTML namespace" xml:lang="en" lang="en">
    <head>
    <title>Test FCKeditor</title>
    </head>
    <body>
    <h1><font size="2" face="Arial, Helvetica, sans-serif"> FCK Editor</font></h1>
    <form action="<?php echo $_SERVER; ?>" method="post" name="editform">
    <table>
    <tr>
    <td>Date</td><td><input type="text" name="date" value="<?php echo; ?>"/></td></tr>
    <tr>
    <td></td>Data<td></td></tr>
    </table>

    <?php
    // Get data from the database
    // Configure and output editor
    $oFCKeditor = new FCKeditor('fcktext');
    $oFCKeditor->BasePath = "fckeditor/";
    $oFCKeditor->Value = $data["data"];
    $oFCKeditor->Width = 540;
    $oFCKeditor->Height = 400;
    echo $oFCKeditor->CreateHtml();
    ?>
    <br />
    <input type="hidden" name="submit_form" value="1" />
    <input type="submit" value="Save Form" />
    </form>
    </body>
    </html>

    <?php
    // Close the database connection
    mysql_close($cnx);
    ?>
    line 21 refers to the date

    thanks again :D


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    <?php echo; ?>

    is neither a command nor a variable, which is causing the PHP parser to go "huh?"

    Also, why are you echoing the date before you retrieve anything from the database ?

    Actually, are you retrieving anything from the database at all ?
    // Get data from the database
    nothing here whatsoever
    // Configure and output editor


  • Registered Users, Registered Users 2 Posts: 287 ✭✭Keewee6


    Liam Byrne wrote: »
    <?php echo; ?>

    is neither a command nor a variable, which is causing the PHP parser to go "huh?"

    Also, why are you echoing the date before you retrieve anything from the database ?

    Actually, are you retrieving anything from the database at all ?

    $query = "SELECT * FROM news WHERE id= $_GET[id]";

    i though this was the retrieving query


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Keewee6 wrote: »
    $query = "SELECT * FROM news WHERE id= $_GET[id]";

    i though this was the retrieving query

    It's the string for a query, but it doesn't do anything to execute the query.

    Have you ever used PHP before ? Or MySQL ?

    In the same way as there is code there to connect to the database....
    mysql_connect("localhost", "**********", ""**********", ")
    OR die("Unable to connect to database!");
    mysql_select_db("*****", $cnx);
    

    ....you need to use a combination of mysql_query and mysql_fetch_assoc to actually "run" the query and send the results back to scope / logic of the "HTML page".

    TBH, you have me completely confused now; your earlier post had some of these commands, and now this one doesn't, and yet you say that your CMS is "almost complete" ?

    From earlier:
    $query = [b]mysql_query[/b]("SELECT data FROM news WHERE id = $id"); 
    $data = [b]mysql_fetch_array[/b]($query);
    

    I don't mean to be rude, but some of the issues that you are encountering are relatively basic (e.g. debugging the PHP and running the query)......that's fair enough, because everyone has to start somewhere, but they're at odds with the post earlier in the thread that your CMS is close to being finished.

    So can you let us know what your level of experience/ability is, so that we can reply in a helpful manner ?


  • Registered Users, Registered Users 2 Posts: 287 ✭✭Keewee6


    i have a low knowledge of mysql/php i am tring to teach myself, built one backend site www.bailieborough.com i want to be able to design a cms using fckeditor im trying to understand - thanks for your help

    has both
    $query = "SELECT * FROM news WHERE id= $_GET[id]";

    and

    $query = mysql_query("SELECT data FROM news WHERE id = $id");
    $data = mysql_fetch_array($query);

    to be in


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Keewee6 wrote: »

    has both
    $query = "SELECT * FROM news WHERE id= $_GET[id]";

    and

    $query = mysql_query("SELECT data FROM news WHERE id = $id");
    $data = mysql_fetch_array($query);

    to be in

    OK - forget about what we TELL you to do, which will have you copying and pasting our code [ without learning anything for the next page ] ; think about what the above commands actually do.

    The first one creates a string containing the query; the query is "SELECT [ all fields ] [ of a specific record FROM news"; , but does nothing with it.

    The second pairing of 2 bits of code creates a slightly different query (only selecting a single field called "data" and also runs it via the mysql_query function and gets the results back in a variable called $data (which in itself might be confusing you because you have a field called "data" in the database, too).

    I would hazard a guess that you need a combination of both, something like :
    $queryString="SELECT * FROM news WHERE id = $id";
    $query = mysql_query($queryString); 
    $dataRecord = mysql_fetch_assoc($query);
    :
    :
    // sample additional code to show how to display some data from the record
    echo $dataRecord["data"];
    

    To be honest, though, you need to stop just cutting and pasting code "in hope" and start to get an understanding of what the code does.


  • Advertisement
Advertisement