-1

I am not sure what I am doing wrong, can anybody tell me?

I have one variable - $tally5 - that I want to insert into database jdixon_WC14 table called PREDICTIONS - the field is called TOTAL_POINTS (int 11 with 0 as the default)

Here is the code I am using. I have made sure that the variable $tally5 is being calculated correctly, but the database won't update. I got the following from an online tutorial after trying one that used mysqli, but that left me a scary error I didn't understand at all :)

if(! get_magic_quotes_gpc() )
{
$points = addslashes ($tally5);
}
else
{
$points = $tally5;
}

$sql = "INSERT INTO PREDICTIONS ".
   "(TOTAL_POINTS) ".
   "VALUES('$points', NOW())";
mysql_select_db('jdixon_WC14');

I amended it to suit my variable name, but I am sure I have really botched this up!

help! :)

Bill Teale
  • 159
  • 3
  • 15
  • Where is `mysql_query`? – u_mulder May 28 '14 at 19:49
  • 1
    *"I got the following from an online tutorial after trying one that used `mysqli`"* --- and you're using `mysql_*` functions? Do show full code. Plus, you're selecting your DB **after** you're querying. – Funk Forty Niner May 28 '14 at 19:50
  • 1
    The query is wrong, you try to put 2 values in one column. – bksi May 28 '14 at 19:50
  • Use mysqli_real_escape_string() instead of addslashes() – David Corbin May 28 '14 at 19:51
  • What version of PHP do you use? The magic quotes are removed as of PHP 5.4.0 and were deprecated long ago. And no: it's not a good idea to use addslashes(). Better use prepared statements with placeholders, i.e. with PDO (or mysqli). – VMai May 28 '14 at 19:56
  • lot of this is missing in your code, first your query is wrong, and your not using mysql_connect and mysql_query function – Nanhe Kumar May 28 '14 at 19:59
  • The real answer => [**Use prepared statements and nothing but.**](http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) – Funk Forty Niner May 28 '14 at 20:02

3 Answers3

0

I think you just need to learn more about PHP and its relation with MYSQL. I will share a simple example of insertion into a mysql database.

<?php
$con=mysqli_connect("localhost","peter","abc123","my_db");

// Check for errors in connection to database. 
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = "INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin',35)";

mysqli_query($con, $query);

mysqli_close($con); //Close connection
?>

First, you need to connect to the database with the mysqli_connect function. Then you can do the query and close the connection

David Corbin
  • 524
  • 2
  • 11
  • 25
0

Briefly,

For every PHP function you use, look it up here first. (You will learn that it is better to go with mysqli).

http://www.php.net/manual/en/ <---use the search feature

  1. Try working on the SQL statement first. If you have the INSERT process down, proceed.

  2. You need to use mysql_connect() before using mysql_select_db()

  3. Once you have a connection and have selected a database, now you my run a query with mysql_query()

When you get more advanced, you'll learn how to integrate error checking and response into the connection, database selection, and query routines. Convert to mysqli or other solutions that are not going to be deprecated soon (it is all in the PHP manual). Good luck!

Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44
-1
if(! get_magic_quotes_gpc() )
{
$points = addslashes ($tally5);
}
else
{
$points = $tally5;
}
mysql_select_db('jdixon_WC14');
$sql = "INSERT INTO PREDICTIONS (TOTAL_POINTS,DATE) ". //write your date field name instead "DATE"
"VALUES('$points', NOW())";
mysql_query($sql);
Dmitry Borovkov
  • 289
  • 4
  • 16