Reputation: 1935
I am attempting to put some form data into two database tables, using the following logic
INSERT
new Watchlist to 'watchlists'
tableSELECT watchlist_id
of new Watchlist from 'watchlists'
table WHERE watchlist_name = $watchlist_name
(name of new Watchlist just created) and user_id = $user_id
INSERT watchlist_id
(selected from previous query) AND film_id
into 'watchlist_films'
tableI am using the following code, but the process seems to break while running the SELECT
query. THe new Watchlist is created in the database fine, but it won't select the ID of the new Watchlist, or run the final section of the code.
if ($db_server) {
// Add new Watchlisth
if (!empty($watchlist_name)) {
$watchlist_name = clean_string($watchlist_name);
$watchlist_description = clean_string($watchlist_description);
mysql_select_db($db_database);
// Create new Watchlist
$insert_new_watchlist = "INSERT INTO watchlists (user_id, name, description, category) VALUES ('$user_id', '$watchlist_name', '$watchlist_description', '$watchlist_category')";
mysql_query($insert_new_watchlist) or die("Insert failed. " . mysql_error() . "<br />" . $insert_new_watchlist);
// Select new Watchlist's ID
$select_new_watchlist_query = "SELECT watchlist_id FROM watchlists WHERE name = " . $watchlist_name;
$new_watchlist_id = mysql_query($select_new_watchlist_query);
// Insert film into new Watchlist
$add_new_film = "INSERT INTO watchlist_films (watchlist_id, film_id) VALUES ('$new_watchlist_id', '$rt_id')";
mysql_query($add_new_film) or die("Insert failed. " . mysql_error() . "<br />" . $add_new_film);
$addWatchlist_good_message = '<div class="alert alert-success">Watchlist created successfully, and film added!</div>';?>
<script>
$('a.add-watchlist').trigger('click');
</script><?php
}
} else {
$addWatchlist_bad_message = '<div class="alert alert-error">Error: could not connect to the database.</div.';?>
<script>
$('a.add-watchlist').trigger('click');
</script><?php
}
require_once("db_close.php");
}
I have run the query in phpMyAdmin using the following string: SELECT watchlist_id FROM watchlists where name = "LotR"
(where LotR is the name of the newly created Watchlist) and that works perfectly, insofar as it brings me back the Watchlist's ID, which is what I want to pass into the second of the two INSERT
queries.
Upvotes: 0
Views: 124
Reputation: 2762
mysql_query()
returns a resource data type, not a field value, so replace
$new_watchlist_id = mysql_query($select_new_watchlist_query);
with
$result=mysql_query($select_new_watchlist_query);
$new_watchlist_id=mysql_result($result,0)
Upvotes: 1