Jump to content

Data not inserting to database


bravo14

Recommended Posts

Hi guys

 

From the page below I am trying to add image 'tags to a database using ajax.

 

<?php
session_start();
$lot_id=$_SESSION['lot_id'];
include_once('includes/connect_inc.php');
?>
<!doctype html public "-//W3C//DTD HTML 4.0 //EN"> 
<html>
<head>
       <title>Trade-Bidz</title>
       <link href="style/trade-bidz.css" rel="stylesheet" type="text/css" />
       <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'></script>
       <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js'></script>
       <script type='text/javascript' src='source/jquery.tag.js'></script>
       <link media="screen" rel="stylesheet" href="css/jquery.tag.css" type="text/css" />
       <link media="screen" rel="stylesheet" href="css/jquery-ui.custom.css" type="text/css" />
</head>
<body>
<table>
<tr>
<td colspan="2">
<?php include_once('page_layout/header_inc.php');?>
</td>
</tr>
<tr>
<tr>
<td colspan="2">
<?php include_once('page_layout/menu_inc.php');?>
</td>
</tr>
<tr>
<td width="200">
<?php include_once('page_layout/left_sidebox_inc.php');?>
</td>
<td>
  <img src="images/car-outline.jpg" id="img" />
</td>
</tr>
<tr>
<td colspan="2">
<?php include_once('page_layout/footer_inc.php');?>
</td>
</tr>
<tr>
</body>
<script>
	$(document).ready(function(){

		$("#img").tag({
			save: function(width,height,top_pos,left,label,the_tag){
				$.post("ajax.php",{'action':'save','width':width,'height':height,'top':top_pos,'left':left,'label':label},function(id){
					the_tag.setId(id);
				});
			},
			remove: function(id){
				$.post("ajax.php",{'action':'delete','id':id});
			}
		});

		$.getJSON("ajax.php",{'action':'list'},function(tags){
			$.each(tags, function(key,tag){
				$("#img").addTag(tag.width,tag.height,tag.top,tag.left,tag.label,tag.id);
			});
		});

	});
</script>
</html>

 

Here is the ajax

 

<?php

/* this is ugly it's just a poc ! Do not use this on production (sql injection...)*/
session_start();
$lot_id=$_SESSION['lot_id'];
include_once('includes/connect_inc.php');

switch($_REQUEST['action']){

	case 'list':
		$tags = array();
		$query = mysql_query("select * from `tbl_tags` where `lot_id` = '$lot_id'");
		while($row = mysql_fetch_array($query)){
			$tags[] = array("id"=>$row['id'],
							"label"=>$row['label'],
							"width"=>$row['width'],
							"height"=>$row['height'],
							"top"=>$row['top'],
							"left"=>$row['left']);
		}
		echo json_encode($tags);
	break;

	case 'delete':
		mysql_query("delete from `tbl_tags where id = ".$_REQUEST['id']);
	break;

	case 'save':
		mysql_query("insert into tag (`width`,`height`,`top`,`left`,`description`,`lot_id`) values (
			".$_REQUEST['width'].",
			".$_REQUEST['height'].",
			".$_REQUEST['top'].",
			".$_REQUEST['left'].",
			'".$_REQUEST['label']."'
                '".$lot_id."
		)") or die(mysql_error());

		echo mysql_insert_id();
	break;

}

?>

 

and the sql to create the table structure is below

 



CREATE TABLE IF NOT EXISTS `tbl_tags` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `lot_id` int(11) NOT NULL,
  `top` int(11) NOT NULL,
  `left` int(11) NOT NULL,
  `width` int(11) NOT NULL,
  `height` int(11) NOT NULL,
  `description` varchar(50) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `lot_id` (`lot_id`)
) ENGINE=MyISAM DEFAULT

 

The records are not inserting into the database,

Link to comment
https://forums.phpfreaks.com/topic/247106-data-not-inserting-to-database/
Share on other sites

Hi,

Try this:

In the Ajax add label as a string (between quotes) 'label':'label'

In the php script replace your query for Insert with this one:

mysql_query("insert into tag (`width`,`height`,`top`,`left`,`description`,`lot_id`) values (
			".$_REQUEST['width'].",
			".$_REQUEST['height'].",
			".$_REQUEST['top'].",
			".$_REQUEST['left'].",
			'".$_REQUEST['label']."',
        ".$lot_id
		))

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.