0

I have a form that allows uploads of images. The user can select which gallery to upload the image to based on a radio button.

In the upload php script, the images are uploaded to a directory then then directory location is added to a MYSQL database. The upload and directory addition works fine.

I'm trying to add the name of the radio button that was pressed to another column in the image database so that I can see which image was uploaded to which gallery as my images are in a separate table.

My form looks like

<form enctype="multipart/form-data" action="upload-image.php" method="POST">
<h3>Select Gallery To Upload To</h3>
<?php
    $results = mysql_query("SELECT * FROM users");
    while ($row = mysql_fetch_assoc($results)) 
    {
        echo'<br>';
        echo '<input type="radio" name="'. $row["username"].'"value="'.$row["username"].'">' . $row["username"];
    }
?>
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

The section of the upload script that handles the adding to the MYSQL database is as follows:

mysql_query("INSERT INTO images (image, gallery_name) VALUES('".$target_path, $_POST["$row["name"]"]."')")

This is giving me a few errors: Notice: Undefined index: name

Warning: mysql_query() expects parameter 2 to be resource, string given

How might I fix this?

4

2 Answers 2

1

Your query's double quotes are a bit awry. You also need to feed mysql_query a resource created by mysql_connect (in the following example, it's represented by $mysql).

mysql_query("INSERT INTO images (image, gallery_name) VALUES('".$target_path."', '". $_POST[$row["name"]]."')", $mysql);
Sign up to request clarification or add additional context in comments.

4 Comments

I've only used this to view items from a database before, EG $results = mysql_query("SELECT * FROM users"); So I'm not 100% sure on whats actually happening here.
@MichaelN what in particular is confusing to you?
If I have say $mysql ("INSERT INTO image etc") then doesn't something have to be done with $mysql afterwards? The only time I've used something like this is to get a row of results, and then echo it out, so after doing $mysql("SELECT* FROM users etc") id then echo $mysql so something was being done with the variable. I hope this makes sense.
For INSERTs mysql_query returns a boolean value based on the success of the query.
1

the string is not concatnated (joined) properly.

change

mysql_query("INSERT INTO images (image, gallery_name) VALUES('".$target_path, $_POST["$row["name"]"]."')")

to

$row_data = $row["name"];
mysql_query("INSERT INTO images (image, gallery_name) VALUES('".$target_path."', '".$_POST[$row_data]."')");

2 Comments

You're almost there, but not quite.
Hmm this seems to give me more errors. Notice: Undefined variable: row Notice: Undefined index:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.