0

I have a simple insert statement that should use $GLOBALS that has my connection string in. Problem is that it does not insert the data. Have I done this correctly?

Insert.php

<?php
require 'core/init.php';

$Name = $_REQUEST["Name"];
$Venue = $_REQUEST["Venue"];
$Category = $_REQUEST["Category"];

$query = "INSERT INTO bands (Name, Venue, Category)
          VALUES ('$Name', '$Venue', '$Category')";
mysql_query ($query,$GLOBALS)
    or die ("could not add to database");

echo("You have added: <br />");
$result = mysql_query("SELECT * FROM bands ORDER  BY  Band_id DESC LIMIT 1");

while($row = mysql_fetch_array($result))
{
    echo $row['Name']. "" . $row['Venue']. "" . $row['Category'];
    echo "<br />";
}
?>
vbo
  • 13,583
  • 1
  • 25
  • 33
Beep
  • 2,737
  • 7
  • 36
  • 85
  • What error do you get? What have you done to troubleshoot this? – John Conde Jan 17 '14 at 14:30
  • 6
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). **You are also wide open to [SQL injections](http://stackoverflow.com/q/60174)** – John Conde Jan 17 '14 at 14:31
  • I am just getting the message "could not add to database", I have run it through a compiler and there are no errors. how would I run further checks? – Beep Jan 17 '14 at 14:32
  • 1
    I know you are starting, but don't use `mysql_*` library and don't use `$_REQUEST`. – Rich Bradshaw Jan 17 '14 at 14:43

2 Answers2

1

You need to connect mysql with mysql_connect() function then select database with mysql_select_db() function then you can call mysql_query function

you just cant execute queries with query string parameter you need to execute that connection query first and then select your database

And i suggest you to use mysqli instead of mysql cuz mysql methods will be deprecated soon
Tutcugil
  • 358
  • 4
  • 11
0

You don't put whole $GLOBALS variable in your mysql_query() call. You put the specific variable with the connection string only:

mysql_query($query, $GLOBALS['connection']) // Assuming you called the connection var "connection"
    or die ("could not add to database");
John Conde
  • 217,595
  • 99
  • 455
  • 496