简体   繁体   中英

Inserting Values into MySQL table with PHP

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

Well... $sqlCmd holds no data, you have to mysql_fetch_array(); (this will fetch the data from query) before starting to manipulate with data.

http://php.net/manual/en/function.mysql-fetch-array.php

Plus, your mysql_query(); syntax is wrong. See manual: http://www.php.net/manual/en/function.mysql-query.php

EDIT:

mysql_select_db("delives0_ads", $con);
$sqlCmd = mysql_query("SELECT * FROM impressions WHERE `something` = '$writeVar';"); // replace something with your needed identifier in database structure
$sqlCmd = mysql_fetch_array($sqlCmd); // will fetch data in array
$yourData = $sqlCmd['what_you_want_to_get_from_table']; // what_you_want..._table is the variable you want to increment.
$yourData++;
mysql_query("UPDATE impressions SET `yourvariable` = '$yourData' WHERE `something` = '$writeVar';"); // updates data
mysql_close($con);

If you could provide database structure, we could help you get the perfect script!

PS Have no idea if this actually works (cannot test, haven't used plain mysql syntax for a long time), but I think it should. In case it doesn't, the problem is at $yourData = $sqlCmd[0]... , you just have to remove the [0] .

A thing kind of off-topic, but still related to you learning php/MySQL:

From what I know when you do queries it's better to add vars like this: '".$var."' instead of '$var'

To understand better what I'm saying I'll show you on your code above. So instead of:

mysql_query("SELECT * FROM impressions ('$writeVar');");

you will have this:

mysql_query("SELECT * FROM impressions ('".$writeVar."');");

I don't remember where I read this, but it was something related to optimizing a query speed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM