According to different register users, add the URL to the database

I have query a row of URL from database “table, book” The code is:

<?php
mysql_select_db(“table”, $db);
$result = mysql_query(“SELECT * FROM book”,$db);
while ($row = mysql_fetch_array($result)) {
$row[“title”];
echo Inc::listing(array(
“”.$row[“url”].“”,
), array(
));}
mysql_free_result($result); ?>

I want to add a bottom end of each URL, when the register user click it, it will write the “url” and “title” to database “table, book2”
book2 structure - Field Name: USER_ID, KEY (number always is 10), URL.
The format of URL is: {title:‘’,catalog:[{title:‘title1’,url:‘url1’},{title:‘title2’,url:‘url2’}]}
//title1 is the “title” witch was added from the PHP page; url1 is the “url” witch was added from the PHP page;

:(The most difficult: If the KEY of the USER_ID have no value(number: 10) in the database, it should created (title:‘’, catalog: [(title: ‘Site Name 1’, url: ‘Website 1’)]);
If the KEY of the USER_ID have already a value(number: 10) in the database, it should add ,{title:‘title2’,url:‘url2’} after {title:‘title1’,url:‘url1’}, to complete {title:‘’,catalog:[{title:‘title1’,url:‘url1’},{title:‘title2’,url:‘url2’}]},
And so on… …{title:‘’,catalog:[{title:‘title1’,url:‘url1’},{title:‘title2’,url:‘url2’},{title:‘title3’,url:‘url3’}]}

If the user has not registered, when he click the bottom, it should be turn to the register page.
Anyone can help me to completed the code that mede me headache? thanks.

RishikeshJha: thanks for your help.
But how to listen the bottom to GET POST the “.$row[“url”].” and “.$row[“title”].” by every registered use id? I am no very sure what your meaning: 1) USE THIS $sql = “insert into jaws_custompages(URL, KEY, USER_ID) VALUES(‘$URL’, ‘10’, ‘$id’)”;

how to complete the ‘else’ part? // write my insert query
php regular express to delete the final three symbol “}]}” then add “,{title:‘name2’,url:‘URL2’}]}” ?

I wrote some code after the first php code:


<script type="application/javascript">
function onBtnClick() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert('add success');
        }
    }
    xmlhttp.open("POST", "favior.php", true);
    xmlhttp.send();
}
</script>
<input name="" type="button" onClick="onBtnClick()"/>

favior.php


<?php
session_start();
$faviorURL = $_SERVER['REQUEST_URI'];
$id = $_SESSION['USERID'];
require_once("config.php");
mysql_select_db("table", $db);
$URL="'\\{'title:'',catalog:'\\[''\\{'title:'name1',url:'$faviorURL''\\}''\\]''\\}'";
$sql = "insert into jaws_custompages(URL, KEY, USER_ID) VALUES('$faviorURL', '10', '$id')";
mysql_query($sql);
?>

I also have some problem:

  1. The $faviorURL which I used just pass the main page’s URL, not the “.$row[“url”].” and “.$row[“title”].” which I need.

  2. I only can write “‘\{‘title:’’,catalog:‘\[’‘\{‘title:‘name1’,url:’$URL’‘\}’‘\]’‘\}’” into the database.
    How to judge if the database had {title:‘’,catalog:[{title:‘name1’,url:‘URL1’}]} and just add ,{title:‘name2’,url:‘URL2’} behind {title:‘name1’,url:‘URL1’} to make a whole address as {title:‘’,catalog:[{title:‘name1’,url:‘URL1’},{title:‘name2’,url:‘URL2’}]}]}

3。 How to judge whether the user has already registed, if not, turn to the register.php?

Thanks again.

  1. USE THIS $sql = “insert into jaws_custompages(URL, KEY, USER_ID) VALUES(‘$URL’, ‘10’, ‘$id’)”;

  2. USE select statement before insert and check whether it is in the table or not like this…
    $sql=mysql_query(select URL from jaws_custompages where URl=‘$URl’);

if(mysql_num_rows($sql)==1{
echo “already exist in database”;

}else{
// write your insert query
}

  1. Check session on this page for registered user.