SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem inserting string into database table

    Hi guys,

    I am new to PHP and SQL and I'm having a bit of a problem with something I am trying to do with Kevin Yank's joke database. I am trying to alter the front end 'add joke' form so that a user can enter their name and email along with their joke. The joketext is successfully inserted into the 'joke' table of my database but I can't get it to add the 'name' and 'email' to the 'author' table as well. I will include the relevant code excerpts below so hopefully someone can point out where I've gone wrong.

    Excerpt from my jokelist/index.php:

    Code PHP:
    if (isset($_POST['joketext']))
    {
    	include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
     
    	$joketext = mysqli_real_escape_string($link, $_POST['joketext']);
    	$sql = 'INSERT INTO joke SET
    			joketext="' . $joketext . '",
    			jokedate=CURDATE()';
    	if (!mysqli_query($link, $sql))
    	{
    		$error = 'Error adding submitted joke: ' . mysqli_error($link);
    		include 'error.html.php';
    		exit();
    	}
     
    	header('Location: .');
    	exit();
    }
     
    if (isset($_POST['name']))
    {
    	include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
     
    	$name = mysqli_real_escape_string($link, $_POST['name']);
    	$sql = 'INSERT INTO author SET
    			name="' . $name . '"';
    	if (!mysqli_query($link, $sql))
    	{
    		$error = 'Error adding author.';
    		include 'error.html.php';
    		exit();
    	}
     
    	header('Location: .');
    	exit();
    }
     
    if (isset($_POST['email']))
    {
    	include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
     
    	$email = mysqli_real_escape_string($link, $_POST['email']);
    	$sql = 'UPDATE author SET
    			email="' . $email . '"
    			WHERE name="$name"';
    	if (!mysqli_query($link, $sql))
    	{
    		$error = 'Error adding authors email.';
    		include 'error.html.php';
    		exit();
    	}
     
    	header('Location: .');
    	exit();
    }

    Excerpt from my jokelist/form.html.php file:
    Code PHP:
    <body>
    	<form action="?" method="post">
    		<div>
    			<label for="joketext">Type your joke here:</label>
    			<textarea id="joketext" name="joketext" rows="3" cols="40">
    			</textarea>
    		</div>
    		<div>
    			<label for="name">Name: <input type="text" name="name"
    				id="name" </label>
    		</div>
    		<div>
    			<label for="email">Email: <input type="text" name="email"
    				id="email" </label>
    		</div>
    		<div><input type="submit" value="Add"/></div>
    	</form>
    </body>

    The form displays correctly with text boxes for all three attributes. The joketext is successfully inserted into my table when i click the 'add' button but the author name and email are not added to the author table.

    Thanks for your time.

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your querys are wrong and you should only have to do one query for author name and email. so erase the name and email portion of your script and add the following.

    they should be more along the lines of this.

    PHP Code:
     if (isset($_POST['name']) && isset($_POST['email']))
    {
        include 
    $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
     
        
    $name mysqli_real_escape_string($link$_POST['name']);
        
    $email mysqli_real_escape_string($link $_POST['email'])
        
    $sql 'INSERT INTO author(name , email) VALUES('.$name.','.$email.')';
        if (!
    mysqli_query($link$sql))
        {
            
    $error 'Error adding author.';
            include 
    'error.html.php';
            exit();
        }
     
        
    header('Location: .');
        exit();

    coming soon sitejuju.com my new development portfolio


Bookmarks

Posting Permissions

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