Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Saving new values (PHP)

  • 27-07-2007 12:46PM
    #1
    Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭


    I'm trying to create a form that edits cells in a table. At the moment I display the form with the values set to what ever is currently in the table so the contents of the appropriate cell can be seen in the text box (kind of like when you click the edit button here). However when I go to UPDATE the database it doesn't accept the edit value instead it takes the original value. Is their anyway arround this or will I have to leave the value blank?

    Heres my script
    [PHP]
    echo "<td><input type=\"text\" name=\"Sname\" value=\"" . $row . "\" size=\"10\" /></td>\n";
    echo "<td><input type=\"text\" name=\"Fname\" value=\"" . $row . "\" size=\"10\" /></td>\n";
    echo "<td><input type=\"text\" name=\"Number\" value=\"" . $row . "\" size=\"10\" /></td>\n";
    [/PHP]

    Thanks,
    S.


Comments

  • Registered Users, Registered Users 2 Posts: 378 ✭✭sicruise


    can you not just use javascript for this?

    document.getElementById('cell1').value=newvalue

    ?


  • Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭Seachmall


    I guess I could but I'd rather not. My JS isn't great plus theirs got to be away to do this without. It seems a bit ludacris.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    I suspect that the form is not where your problem lies. When you submit the form, are you correctly taking the POSTed data and using that (and not the data from the database) in your UPDATE?

    Have you also checked your UPDATE syntax? If it fails silently you might get the impression that all you're doing is using the original values (as they've not changed).


  • Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭Seachmall


    I hadn't thought of the possibility that its not working:o . I've gone through the script many-a-time though and can't find any errors in it. Heres the form:
    [PHP]
    echo "<form name=\"edit\" method=\"post\" action=\"editVal.php\">";
    echo "<tr>";
    echo "<td><input type=\"text\" name=\"day\" value=\"" . $row . "\" size=\"8\" /></td>\n";
    echo "<td><input type=\"text\" name=\"Sname\" value=\"" . $row . "\" size=\"10\" /></td>\n";
    echo "<td><input type=\"text\" name=\"Fname\" value=\"" . $row . "\" size=\"10\" /></td>\n";
    echo "<td><input type=\"text\" name=\"Number\" value=\"" . $row . "\" size=\"10\" /></td>\n";
    echo "<td><input type=\"submit\" value=\"Done\" />\n";
    echo "</form>\n";
    echo "</td></tr>\n";
    }
    mysql_close($con);
    [/PHP]

    And the UPDATE:
    [PHP]<?php
    mysql_query("UPDATE table_name SET
    Sname = '$_POST[Sname]',
    Fname = '$_POST[Fname]',
    number = '$_POST[number]',
    WHERE IdNo='$_POST[IdNo]' AND day='$_POST[day]'");
    ?>[/PHP]

    Everything appears to be in order. Also, how do I test to see if its not working if I don't store the command in a variable? Is it something like
    [PHP]
    if (mysql_query==false){
    echo "error";
    }[/PHP]?

    Thanks,
    S


  • Closed Accounts Posts: 45 CelloPoint


    Seachmall wrote:
    I hadn't thought of the possibility that its not working:o . I've gone through the script many-a-time though and can't find any errors in it. Heres the form:
    [PHP]
    echo "<form name=\"edit\" method=\"post\" action=\"editVal.php\">";
    echo "<tr>";
    echo "<td><input type=\"text\" name=\"day\" value=\"" . $row . "\" size=\"8\" /></td>\n";
    echo "<td><input type=\"text\" name=\"Sname\" value=\"" . $row . "\" size=\"10\" /></td>\n";
    echo "<td><input type=\"text\" name=\"Fname\" value=\"" . $row . "\" size=\"10\" /></td>\n";
    echo "<td><input type=\"text\" name=\"Number\" value=\"" . $row . "\" size=\"10\" /></td>\n";
    echo "<td><input type=\"submit\" value=\"Done\" />\n";
    echo "</form>\n";
    echo "</td></tr>\n";
    }
    mysql_close($con);
    [/PHP]

    And the UPDATE:
    [PHP]<?php
    mysql_query("UPDATE table_name SET
    Sname = '$_POST[Sname]',
    Fname = '$_POST[Fname]',
    number = '$_POST[number]',
    WHERE IdNo='$_POST[IdNo]' AND day='$_POST[day]'");
    ?>[/PHP]

    Everything appears to be in order. Also, how do I test to see if its not working if I don't store the command in a variable? Is it something like
    [PHP]
    if (mysql_query==false){
    echo "error";
    }[/PHP]?

    Thanks,
    S

    Yeah, in this case, I think you'd need to use the PHP DOM (although watch out for server load if your site is gonna be used a lot). I tried to go down the all-PHP road once myself, but echoing JavaScript via PHP was just way more convenient.

    Some people go way over-the-top about browser compatibility: I think most people have JS in this day and age. What is it you're trying to achieve that you'd prefer not to use javascript?


  • Advertisement
Advertisement