0

Sorry if my english is not correct. I will try my best.. The question is how can I insert record from existing table with insert button. Everything work fine except variable $book. It records value 0 to database. Here is code..

//if the user press "INSERT BOOK" button there will be new record in database
<?php

$reader=$_SESSION['user_id'];
$book=$row['book_id'];

if (isset($_POST['update'])){
    $sql = "INSERT INTO `read` (`read_id`, `book`, `reader`) VALUES (LAST_INSERT_ID(), '$book' '$reader')";
    $result = mysql_query($sql) or die(mysql_error());

    if ($result) {
        echo " Yes, it works!";
    }
    else{
        echo "noup!";
    }
}

if(isset($_POST['submit'])){ 
    if(isset($_GET['go'])){ 
        if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){ 
            $name=$_POST['name'];

            $result = mysql_query("SELECT * FROM book b JOIN `read` r ON (b.book_id = r.book) JOIN users u ON (r.reader = u.user_id)    WHERE b.book_name LIKE '%" . $name .  "%' or b.writer LIKE '%" . $name .  "%' AND u.user_id !=". $_SESSION['user_id']);

            $num_rows = mysql_num_rows($result);
            if($num_rows>=1){
                echo "<table id='table1'>
                          <tr>
                              <th>book_id</th>
                              <th>BOOKNAME</th>
                              <th>WRITER</th>
                              <th>PAGES</th>
                          </tr>";

                while($row=mysql_fetch_array($result)){

                echo "<form action=add_book.php method='post'>";
                echo "<tr>";
                echo "<td>" . $row['book_id'] . "</td>";
                echo "<td>" . $row['book_name'] . "</td>";
                echo "<td>" . $row['writer'] . "</td>";
                echo "<td>" . $row['pages'] . "</td>";

              //echo "<td>" . "<input type = 'hidden' name = 'hidden' value ="  . $row['book_id'] . " </td>";
                echo "<td>" . "<input class = 'field' type = 'submit' name = 'update' value = 'INSERT BOOK'>" . " </td>";
                echo "</tr>";
                echo "</form>";
                }//WHILE LOOP
            echo  "</table>";
            }
            else{
                echo "NO RESULTS";
            }//else
        }//if(preg_match..)
    } //if(isset($_GET['go'])){ 
} //if(isset($_POST['submit'])){
?>
Bono
  • 4,757
  • 6
  • 48
  • 77
hannu
  • 1
  • 7
  • 1
    **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** – Jay Blanchard Jan 19 '15 at 17:59
  • 1
    you have this line at the top: `$book=$row['book_id'];` but where is `$row` defined other than *after* that line? – Dan Smith Jan 19 '15 at 18:00
  • Many thanks! The problem is that I cannot define that variable. Can you give me example of that. I'm just learning these things.. – hannu Jan 19 '15 at 18:18
  • @hannu `$row` is only available after you've called `mysql_fetch_array()` in that while loop. You're using it at the very top of you're page, where it hasn't got any value (or was set) yet. I think you're looking for `$_POST`. You'll have to make some HTML input fields and get their value using PHP. – Bono Jan 19 '15 at 18:41
  • Thank you Bono. yes, there is html input fields at the beginning. Did you mean by that? I didnt put that code to the question text because I thought it is not necessary. – hannu Jan 19 '15 at 19:45

0 Answers0