提交表格并将其发布到主页

时间:2016-05-18 19:04:03

标签: javascript php jquery mysql ajax

我有这个表格form image。提交表格后,个人信息将存入个人表格并将书籍详细信息存入书籍表格。 通过id。在两个表之间有一个链接。

$user_query = "INSERT INTO personaldetails(FirstName ,MiddleName,LastName,
        Gender,Location,Email,Mobile) VALUES ('$users_firstname',
        '$users_middlename', '$users_lastname', '$users_gender','$users_location','$users_email','$users_mobile');";


       $result = mysqli_query($mysqli, $user_query);

        $user_id = mysqli_insert_id($mysqli);


        foreach($_POST['booktitle'] as $key => $bookTitle) {
            $bookTitle = mysqli_real_escape_string($mysqli, $bookTitle);
            $bookGenre = mysqli_real_escape_string($mysqli, $_POST['bookgenre'][$key]);
            $bookWriter = mysqli_real_escape_string($mysqli, $_POST['bookwriter'][$key]);
            $bookDescription = mysqli_real_escape_string($mysqli, $_POST['bookdescription'][$key]);

            $book_query = "INSERT INTO bookdetails(BookTitle ,BookGenre,BookWriter,
                BookDescription, UserId) VALUES('$bookTitle',
             '$bookGenre', '$bookWriter', '$bookDescription', '$user_id');";

我的要求是,当我点击发布按钮时,我插入个人详细信息和书籍详细信息的数据应该发布在主页上(相同的Facebook状态更新)

我试图将这两个表的数据作为jason回显,但它正在获取表的整行。

$sql1 = mysql_query("select * from personaldetails");

    $sql2 = mysql_query(" select BookTitle,BookGenre,BookWriter,BookDescription from bookdetails INNER JOIN personaldetails ON (bookdetails.UserId = personaldetails.Id)"); 

echo '{"personaldetails": [';
        while($row=mysql_fetch_array($sql1))
        {
            $id=$row['Id'];
            $fname=$row['FirstName'];
            $mname=$row['MiddleName'];
            $lname=$row['LastName'];
            $gender=$row['Gender'];
            $location=$row['Location'];
            $email=$row['Email'];
            $mobile=$row['Mobile'];
    echo '
        {
            "Id":"'.$id.'",
            "FirstName":"'.$fname.'"
            "MiddleName":"'.$mname.'"
            "LastName":"'.$lname.'"
            "Gender":"'.$gender.'"
            "Location":"'.$location.'"
            "Email":"'.$email.'"
            "Mobile":"'.$mobile.'"
        },'; 
        }
    echo ']}';

    echo '{"bookdetails": [';
        while($row=mysql_fetch_array($sql2))
        {

            $btitle=$row['BookTitle'];
            $bgenre=$row['BookGenre'];
            $bwriter=$row['BookWriter'];
            $bdescription=$row['BookDescription'];

    echo '
        {

            "BookTitle":"'.$btitle.'"
            "BookGenre":"'.$bgenre.'"
            "BookWriter":"'.$bwriter.'"
            "BookDescription":"'.$bdescription.'"

        },'; 
        }
    echo ']}';

?>

我有一点想法,我可以通过jquery或ajax调用api但不知道该怎么做。或者我也可能需要url params。 如果您有任何想法,请帮助我。我将不胜感激。

1 个答案:

答案 0 :(得分:0)

查看代码,您需要做很多事情。首先是..自己停止写JSON!接下来是您的查询。

$sql1 = mysql_query("select * from personaldetails");

while($row=mysql_fetch_array($sql1)) {
     $output['personaldetails'][] = $row;
}

$sql2 = mysql_query(" select BookTitle,BookGenre,BookWriter,BookDescription from bookdetails INNER JOIN personaldetails ON (bookdetails.UserId = personaldetails.Id)"); 

while($row=mysql_fetch_array($sql2)) {
     $output['bookdetails'][] = $row;
}

echo json_encode($output);
?>