Results 1 to 3 of 3

Thread: Insert data in to two tables in mysql

  1. #1
    Join Date
    Nov 2013

    Insert data in to two tables in mysql

    Hi, i have two tables in my database, one named jokes and the other named category. I want the user to select a category and write a joke regarding that category.

    here are my jokes tables.

    HTML Code:
    CREATE TABLE `jokes` (
      `joke_id` int(11) NOT NULL AUTO_INCREMENT,
      `joke` varchar(1024) NOT NULL,
      `category_id` int(11) NOT NULL,
      PRIMARY KEY (`joke_id`)
    here is my category table.

    HTML Code:
    CREATE TABLE `category` (
      `category_id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(51) NOT NULL,
      PRIMARY KEY (`category_id`)
    and here is the create.php file which will add data to the database. So far i am able to just add a joke description to the database, without any reference to a category, which is my main problem, as i want to create category specific pages which the user uploaded jokes.

    HTML Code:
    	require 'database.php';
    	if ($_POST) {
    		// keep track validation errors
    		$jokeError = null;
                    $categoryError = null;
    		// keep track post values
    		$joke = $_POST['joke'];
                    $category = $_POST['category'];
                    $celebrity = $_POST['celebrity'];
                    $news = $_POST['news'];
    		$short = $_POST['short'];
    		// validate input
    		$valid = true;
    		//initiate error message if field is empty
    		if (empty($joke)) {
    			$jokeError = 'Please enter a Joke';
    			$valid = false;
                    if (empty($category)) {
    			$categoryError = 'Please enter a Category';
    			$valid = false;
    		// insert data
    		if ($valid) {
                        $pdo = Database::connect();
                        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                        $sql = "INSERT INTO jokes (joke, category_id) values(?,?)";
                        $q = $pdo->prepare($sql);
                        $q->execute(array($joke, $category_id));
                        header("Location: index.php");
    <!DOCTYPE html>
    <html lang="en">
        <meta charset="utf-8">
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <link   href="css/bootstrap.css" rel="stylesheet">
        <link rel="stylesheet" media="screen, projection" href="fancySelect.css">
        <div class="container">
        			<div class="span10 offset1">
        				<div class="row">
    		    			<h3>Create a Joke</h3>
    	    			<form class="form-horizontal" action="create.php" method="post">
                                        <div class="control-group">
                                            <label class="control-label">Category</label>
                                            <div class="controls">
                                                <select class="category" name="category">
                                                        <option value="">Select something…</option>
                                                        <option name="celebrity" value="<?php echo $celebrity;?>">Celebrity</option>
                                                        <option name="news" value="<?php echo $news;?>">News</option>
                                                        <option name="short" value="<?php echo $short;?>">Short</option>
    					  <div class="control-group <?php echo ($jokeError)?'error':'';?>">
    					    <label class="control-label">Joke</label>
    					    <div class="controls">
    					      	<textarea name="joke" type="text"  placeholder="Enter a short synopsis" class="field span6 " rows="6"><?php echo $joke;?></textarea>
    					      	<?php if ($jokeError): ?>
    					      		<span class="help-block"><?php echo $jokeError;?></span>
    					      	<?php endif;?>
    					  <div class="form-actions">
    						  <button type="submit" class="btn btn-success">Create</button>
    						  <a class="btn" href="index.php">Back</a>
        </div> <!-- /container -->
    or if it makes it easier to edit, here is the create.php file in github: https://github.com/sambarrowclough/jokes.git

    Thank you for any help.

  2. #2
    Join Date
    Jul 2013
    Voorheesville NY USA
    How about some idea what your real problem is? Or are you just looking for development help and not error resolution?

  3. #3
    Join Date
    Sep 2013
    I have used the below code for inserting as compared to the second tables:

    INSERT INTO tbl_temp2 (fld_id)
    SELECT tbl_temp1.fld_order_id
    FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

    If i have mistaken in clearing your doubts so can you please post your database error....

    Hope this helps.
    strad solutionswww.stradsolutions.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles