oldmanpete
oldmanpete

Reputation: 57

PHP and html select tag not writing into DB

Firstly forgive the use of mysql_*, I appreciate it is depreciated but for completely ridiculous reasons I have to use it for this project.

So I have a dropdown menu populated out of my database with the use of this function

func.php

    function artistQuery(){
    $myData = mysql_query("SELECT * FROM artists");
    while ($record = mysql_fetch_array($myData)){
    echo '<option value="' . $record['artistID'] . '">' . $record['artistFirst'] . ' ' . $record['artistSurname'] . '</option>';
    }

It populates the drop down menu on:

newprod.php

<h1>New Product Entry:</h1>
<form action="php/addproduct.php" method="_POST">

<p>Please select the artist:</p>

<select name="artist">
<?php artistQuery();
?>
</select>
</form>

So from my function although the first name and surname of the artist are displayed the option value is actually the artistID.

I then use

addproduct.php

<?php

    include_once 'php/dbconn.php';

    connect();


$artist = $_POST['artist'];

$query = "INSERT INTO products ('artistID') VALUES ('$artist')";

//execute query
$result = mysql_query($query) or die("Error in Query. $query. " . mysql_error());

echo "$artist";

    ?>

To write the artistID into the database, except it isn't writing.

I am assuming the issue lies somewhere in the newprod.php (Middle block of code) not assigning the artistID to the name of 'artist'.

Any and all help from you wonderful people would be appreciated.

EDIT: Missing letters!

Upvotes: 2

Views: 125

Answers (4)

Salvador P.
Salvador P.

Reputation: 1489

To avoid some sql injections try to use mysql_escape_string

$query = "INSERT INTO products ('artistID') VALUES ('".mysql_escape_string($artist)."')";

Check if $artist has value, maybe is not inserting in the db, because is null. Or maybe is not the value the db is expecting, like artistID is an integer an $artist actually is a string.

Upvotes: 0

jacouh
jacouh

Reputation: 8741

In addproduct.php, try this:

$query = "INSERT INTO products (artistID) VALUES ('$artist')";

Instead of quoted column name (bad):

$query = "INSERT INTO products ('artistID') VALUES ('$artist')";

MySQL column name can be neither single quoted 'artistID', nor double quoted "artistID", nor [artistID] like in Access Database. It must be either bare name artistID, or anti-quoted like

`artistID`

It's useful when you have spaces in the column name like

`Artist Name`

.

Upvotes: 1

Gerald Schneider
Gerald Schneider

Reputation: 17797

It should be method="POST", not method="_POST".

_POST is invalid and is ignored by the browser. The browser will then use GET as method, which is the default for HTML forms.

Edit: The answer by @jacouh is also valid, you have to do both his and my change to make it work.

Upvotes: 1

praveen
praveen

Reputation: 286

Inside select box you should give some thing like this

<select name="artist">
<option value="artist"><?php artistQuery();?></option>
</select>

Upvotes: -1

Related Questions