-8

i'm new to PHP development

i face some issue when i try to submit a data to the database here is my code which i'm trying to insert into a db name topic and image.

$user = '1';
    $title = mysql_real_escape_string($_POST['title']);
    $msg = mysql_real_escape_string($_POST['msg']);
    $date = date('Y-m-d H:i:s');

    $img= "";
    $iname = $_FILES['file_1']['name'];
    $temp4 = $_FILES['file_1']['tmp_name'];
    move_uploaded_file($temp,"upload/".$iname);
    $path = "upload/".$iname;

    if (isset($_POST['submit']))
    {
    $sql = mysql_query("INSERT INTO topic (UserID, Title, MsgBody, DateTime)     VALUES('".$user."', '".$title."', '".$msg."', '".$date."')");
    $sql2 = mysql_query("INSERT INTO image (TopicID, ImgPath)    VALUES('".mysql_insert_id()."', '".$path."')");

   }
   else 
   {
    echo "error";
   }    
  • 3
    What issue do you face? Do you get an error? No data inserted? The wrong data? Some of the right data? – andrewsi Oct 23 '13 at 13:10
  • also you should really be using mysqli not mysql – Jonnny Oct 23 '13 at 13:11
  • [Why you shouldn't use mysql_* functions in PHP.](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – George Oct 23 '13 at 13:11
  • no data inserted. i tried insert data by $title='test'; $msg='test';$path='/upload/1.jpeg'; the query works – Ervin Lew Oct 23 '13 at 14:32
  • could you please add the error , and also the tables schema . – iskandarm Oct 23 '13 at 14:33
  • @ErvinLew - there's no error checking in your code, at all. If your calls to `move_uploaded_file()` or `mysql_query()` don't work, you'll never know. You need to be checking what the return value of those functions are, and dealing with it as required. If your database queries are failing, then look in `mysql_error()`, and there'll be an error message from the database telling you what the issue is. – andrewsi Oct 23 '13 at 14:39
  • @andrewsi - as i meantion i'm new to this. i did add $dbhost = "localhost"; $dbname = "mdb_kl237"; $dbuser = "root"; $dbpass = ""; mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " .mysql_error()); mysql_select_db($dbname) or die("MySQL Error: " .mysql_error()); – Ervin Lew Oct 23 '13 at 14:53
  • @ErvinLew - you can add `or die(mysql_error())` after each of your calls to `mysql_query()`, too. You can add it as often as you need to - it's useful to add it whenever you're running queries. – andrewsi Oct 23 '13 at 14:55

2 Answers2

0

use transaction in this case where you need to do more tha 1 query.the link is given below Transaction from stackoverflow

Community
  • 1
  • 1
0

Everything in your code seem's to be fine except mysql_insert_id function. Depending on what PHP version you use, it is deprecated since PHP 5.5.0. See the documentation.

matewka
  • 9,912
  • 2
  • 32
  • 43