Inserting values ​​into the MySQL table with PHP

advertisements

I have a PHP array that has several values in it, corresponding to different banner ads. Let's say $a is called, which would be the 0th value in the array. I have a field in the table impressions, likewise called 0, that holds an integer. I want to call this integer from this field, increment it, and put it back in the same row.

To oversimplify. I want to call up the number of impressions the current ad shown has. Say ad $a has 500 impressions. I want to increment that number, to 501, and put it back in the table.

I hope I explained well enough. Here's my code so far:

$ads = array($a, $b, $c, $d, $e, $f, $g, $h, $i, $j);
$rand = rand(0,9);
print($ads[$rand]);
$writeVar = $rand;

$con = mysql_connect("localhost","delives0_ads","ads");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("delives0_ads", $con);
$sqlCmd = mysql_query("SELECT * FROM impressions ('$writeVar');");
$sqlCmd++;
mysql_query("INSERT INTO impressions ('$writeVar`) VALUES ('$sqlCmd');");
mysql_close($con);

Edit: Here is the structure in SQL

CREATE TABLE IF NOT EXISTS `impressions` (
  `0` int(11) NOT NULL,
  `1` int(11) NOT NULL,
  `2` int(11) NOT NULL,
  `3` int(11) NOT NULL,
  `4` int(11) NOT NULL,
  `5` int(11) NOT NULL,
  `6` int(11) NOT NULL,
  `7` int(11) NOT NULL,
  `8` int(11) NOT NULL,
  `9` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


You do not need a SELECT and UPDATE query to increment a column in a table.

Just send one UPDATE query [example]:

UPDATE impressions SET views = views + 1 WHERE ad_id = 3

Throw out all the code you've written as none of what you posted is correct. Definitely do not use the database table you designed.

If I might suggest a book or two:

http://www.amazon.com/Build-Database-Driven-Using-MySQL/dp/0980576814/ref=dp_ob_title_bk

http://www.amazon.com/Simply-SQL-Rudy-Limeback/dp/0980455251