0

I have a form where the user enters the url they want and proceeds to the next step. This is all done with no page refresh, so I'm relying on Ajax to send the information. When the user submits the url they want, they are allowed to go the next part. The user also has the option to go back to the url page and change their url. I know how to insert the url successfully, but I don't know how to update the entry with the new url. I tried this, but it's not working:

$prevUrl;
$newUrl;

if ((isset($_POST["url"]))
{
    $newUrl = $_POST["url"];

if ($prevUrl == "")
{
   $db->query("INSERT INTO Articles (`url`) VALUES ('$newUrl')");
   $prevUrl = $newUrl;
}

else
{
   $db->query("UPDATE Articles SET url = '$newUrl' WHERE url = '$prevUrl'");
   $prevUrl = $newUrl;
}
}

How can I make it so that when the entry is updated, it gets updated based on what the previous url was?

What's wrong: When I go back to update the url, the url doesn't get updated with the new one. My WHERE clause is not executing successfully. It is as though the $prevUrl variable is empty. I figure that since PHP is server side, whatever variables are shown upon page refresh, those are the variables and they can't change. Since $prevUrl is empty at first, the WHERE statement is probably doing something like this WHERE url = "". I'm not sure if that's it though. There was no exception error or any kind of fatal error.

user2896120
  • 3,180
  • 4
  • 40
  • 100
  • Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please [edit] these details in or we may not be able to help. – Blue Jul 30 '16 at 23:43
  • @FrankerZ I just updated the question with those details – user2896120 Jul 30 '16 at 23:50
  • I see tons of typos: `es (\`url) ` (Missing a \`). `$db-query("UPDATE Articles` (Missing a `>` before query. How does this code work at all? See [this](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) post and enable `error_reporting` and post back with actual errors. – Blue Jul 30 '16 at 23:51
  • @FrankerZ Sorry, I just typed the code up in my question. This isn't my real code, it's a simplified version of it. The only difference with this code and my code though, is that my code contains more values in the update statement – user2896120 Jul 30 '16 at 23:54
  • Looking at this code, it seems to be correct. The easiest and simplest way to debug queries is to echo them out. Do they look correct? Run them against the database via phpMyAdmin or the console. Do you get the expected result? Make sure you're checking for errors through mysql (Which you shouldn't be using anymore, BTW. See PDO/MySQLi, and [this](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) for more info). – Blue Jul 30 '16 at 23:59
  • @FrankerZ I use MySQLi, but like I said my page never refreshes, so if I echo $prevUrl and refresh the page, there will be no output because $prevUrl is empty at first. It gets assigned a value only when the user hits submit. – user2896120 Jul 31 '16 at 00:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118720/discussion-between-frankerz-and-user2896120). – Blue Jul 31 '16 at 00:03

1 Answers1

0

I don know, if I got you complete right, but what I think you are trying to do is not possible. An Ajax Request is nothing else but executing some code on the Server and sending the result back, without reloading. After sending the results there are no acessable variables left on the server, exept of $_SESSION.

Your variable $prevurl can not be anything else but 'null', because at the beginning of your code you declare it like that.

In your case there are two possibilities, to solve that. Either you write the URL in the $_SESSION variable:

session_start();
$_SESSION['url']= $newurl;

and acess it like that again.

Or you modify your table 'Articles' so that you can better check if there is already an 'url'-entry and you therefore have to UPDATE rather than INSERT.

EDIT:

specify my first suggestion:

The $_SESSION variable can be very much changed via ajax. If you want to only use ajax to live-submit data from user, it is possible, but you just can't work with <form>-elements. You have to work with javascript:

<!-- HTML -->
<input id='newurl' />
<input type='button' value='next' onclick='submitURL(document.getElementById("newurl").value)' />

// JAVASCRPT
function submitURL(newurl){
    var xhttp;
    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
        } else {
        // code for IE6, IE5
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhttp.open("GET", /*include url_submit.php?data=newurl*/, true );
    xhttp.send();

    xhttp.onreadystatechange = function(){
        if(xhttp.readyState==4&&xhttp.status == 200){
            //change Pagecontent to next page
        }
    };
}

# PHP (url_sumbit.php)
<?php
if(!isset($_SESSION['url']){
    session_start();
}
$_SESSION['url']=$_GET['data'];
?>

When all user data is submitted, a .php-file is included, that creates the database entries.

specify second suggestion:

in the .php-file, instead of saving the user data in $_SESSION, it is immediately written into a datatable. Promlem is, that you don't know if you have to update or insert(if the url has been submitted before or not). So you have to check manually, if there is an entry, if yes update it, if no insert a new one.

I have to say that both ways are very ugly. It would be much more easier when all data is submitted at once the end. For example, if you insist on not-reloading, write some javascript, that dont submit when clicking the 'next' button but replaces the old pagecontent and stores the 'submitted' data in a hidden <input>. At the end, all is submitted properly and added to the database.

Eierschale
  • 43
  • 5
  • Well at first there won't be a url entry because the user will make it themselves. They will go on the non-refresh page and type in the url they want and click submit, the first time they submit it'll create a new entry. They move on to the next page (keep in mind this is one page, but it never refreshes). Let's say they decided to change their url, so they go back. They change the url and their url should change based on their key previous url. SESSION PHP variables are static and don't change unless the page refreshes. – user2896120 Jul 31 '16 at 05:38