Scroll page back to the top

Save search requests to MySQL database

Save search requests to MySQL database

In this tutorial we will have a look at the way of saving search requests submitted through the online search form into the MySQL database using PHP PDO and INSERT INTO ... ON DUPLICATE KEY UPDATE MySQL statement.

Time: 14:03 min

Sebastian Sulinski on 25th Nov 2011

Download Exercise Files
 
 
Watch now!

Database design (04:56 min)

 
Watch now!

Fetch records (05:14 min)

 
Watch now!

ON DUPLICATE KEY UPDATE (04:33 min)

 
 
 
 

Discussion (5 comments)

  • Stan Valentin Cristian

    Stan Valentin Cristian on Friday, 25th November 2011

    Very nice and very useful tutorial, however I already have a script with the config file and I don't want to use PDO.
    How can i use it without PDO with the following:

    	$statement = $objDb->prepare($sql);
    	$statement->execute(array(':keyword' => $keyword));
    } catch(PDOException $e) {
    	echo $e->getMessage();
    }
    
    $statement = $objDb->query($sql);
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    

    Any idea?

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Sunday, 27th November 2011

    Hi Stan,
    Here's the example with the standard mysql extension:

    <?php
    $username = "database_username";
    $password = "database_password";
    $database = "database_name";
    $server = "localhost";
    
    $dbconnect = mysql_connect($server, $username, $password);
    $dbselect = mysql_select_db($database, $dbconnect);
    
    if ($dbselect) {
    
    	$sql = "SELECT `s`.*,
    		IF (
    			(
    				SELECT `id`
    				FROM `search`
    				ORDER BY `date` DESC
    				LIMIT 0, 1
    			) = `s`.`id`,
    			1,
    			0
    		) AS `latest`
    		FROM `search` `s`
    		ORDER BY `s`.`keyword` ASC";
    	$result = mysql_query($sql);
    	
    }
    ?>
    
    <?php if (!empty($result)) { ?>
    	
    	<table cellpadding="0" cellspacing="0" border="0" class="tbl_repeat">
    		<tr>
    			<th>Keyword</th>
    			<th class="tar">Searched</th>
    		</tr>
    		<?php while ($row = mysql_fetch_assoc($result)) { ?>
    		<tr<?php echo $row['latest'] == 1 ? ' class="active"' : null; ?>>
    			<td><?php echo $row['keyword']; ?></td>
    			<td class="tar"><?php echo $row['times']; ?></td>
    		</tr>
    		<?php } ?>
    	</table>
    	
    <?php } else { ?>
    	<p>There are currently no searches available.</p>
    <?php } ?>
    
    <?php mysql_close($dbconnect); ?>
    

    Reply

  • Stan Valentin Cristian

    Stan Valentin Cristian on Sunday, 27th November 2011

    Thank you very much

    Reply

  • Dan Farrell

    Dan Farrell : @farreal on Thursday, 8th March 2012

    Hi Sebastian, in the first video you had a live results and count section. Im looking to add that exact thing into my site how did you make yours?

    Reply

  • Sebastian Sulinski

    Sebastian Sulinski : @designtutorials on Friday, 16th March 2012

    Hi Dan,
    It's all explained in the video - if you watch the whole series you'll see how I've done it.

    Reply

 
 
Add a comment
Add Comment