1

I need to save content into two tables. I have this code, but it save just into table games. I need save into the games all content and into archive just id, title and date of article.

Any help?

Thanks very much

 <?php include("../includes/connect.php"); if(isset($_POST['submit'])) { $games_date = date('y-m-d-h'); $games_title = $_POST['title']; $games_author = $_POST['author']; $games_keywords = $_POST['keywords']; $games_link = $_POST['download_link']; $games_image = $_FILES['image']['name']; $games_tmp = $_FILES['image']['tmp_name']; $games_content = $_POST['content']; if($games_title=="" or $games_author=="" or $games_keywords=="" or $games_content=="" or $games_download_link="") { echo"<script>alert('any field is empty')</script>"; exit(); } else { move_uploaded_file($games_tmp,"../uploaded_images/$games_image"); $games_query = "insert into games (games_title,games_date,games_author,games_image,games_keywords,games_link,games_content) values ('$games_title','$games_date','$games_author','$games_image','$games_keywords','$games_link','$games_content')"; $last_id = mysqli_insert_id($connect); $archive_query = "insert into archive (title, pub_date) values ('$games_title','$games_date',)"; } if(mysqli_query($connect,$games_query)) { echo "<center><h1>Post published seccesfuly!</h1></center>"; } } ?> 
4
  • 1
    I only see that you are running the $games_query and not the $archive_query. Are you missing some of the code? Commented Mar 22, 2014 at 15:08
  • No that's all from this script. I think that i was missing something what but i don't know is it. Commented Mar 22, 2014 at 15:12
  • You are not executing the $archive_query. It's not going to jump up and magically execute itself. Your code is also vulnerable to SQL injection, making it potentially possible for someone to corrupt your database, steal information from it, or take control of the site. Commented Mar 22, 2014 at 15:33
  • yes i know that isn't safe...now i just need to make it work Commented Mar 22, 2014 at 15:40

2 Answers 2

2

As you have mentioned that you need to insert the id, I'd prefer to go with this way

.... if(mysqli_query($connect,$games_query)){ $last_id = mysqli_insert_id($connect); $archive_query = "insert into archive (id, title, pub_date) values ('$last_id', '$games_title','$games_date')"; mysqli_query($connect,$archive_query); // execute the archive query here echo "<center><h1>Post published seccesfuly!</h1></center>"; } } 
Sign up to request clarification or add additional context in comments.

Comments

1

You could use mysqli::multi_query

$query = "insert into games (games_title,games_date,games_author,games_image,games_keywords,games_link,games_content) values ('$games_title','$games_date','$games_author','$games_image','$games_keywords','$games_link','$games_content');"; $query .= "insert into archive (title, pub_date) values ('$games_title','$games_date')"; if (mysqli_multi_query($connect, $query)) { echo "Data in two tables successfully inserted."; } 

8 Comments

Can you post the error message? Also, did you check out the manual link I gave you ?
there is no error...it will show Post published seccesfuly and all content is in games. Yes i checked
Did it print Data in two tables successfully inserted. ?
ou...you mean your solution...there is error: Fatal error: Call to a member function mutli_query() on a non-object in line 76 (if($mysqli->mutli_query($games_query)) { echo "<center><h1>Post published seccesfuly!</h1></center>"; } }
@psajtik, Sorry .. can you try now ? I have changed the if condition.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.