Search the Community
Showing results for tags 'insert'.
-
Hello again, I'm wondering if it is possible to insert to a table and then update to that table in the same php script. What I want to do is insert a location address, then geocode it with a location update immediately after the insert with the form action as the same page. However, I suspect that I will need to do this through a hidden submit input that takes me to another page because I have been scratching my head over it for a few hours now. Here is the location insert $q = 'INSERT INTO location (loc_address, loc_address2, loc_city, loc_state, loc_zip, loc_country, coll_id, country_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'; $stmt = mysqli_prepare($dbc, $q); mysqli_stmt_bind_param($stmt, 'ssssssii', $addr1, $addr2, $city, $state, $zip, $sn, $c, $coun); mysqli_stmt_execute($stmt); //Check the results if (mysqli_stmt_affected_rows($stmt)== 1){ echo '<p>The location has been added</p>'; $locid = mysqli_stmt_insert_id($stmt);//Get the location ID }else{//Error! $errors[] = 'the location could not be added to the database.'; } //Close this prepared statement mysqli_stmt_close($stmt); and here is the geocoding with the update query //geocode it $geoloc="$addr1, $city, $state, $zip, $sn"; $georesult=file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" . urlencode($location) ); $geocodedinfo=json_decode($result); print_r($geocodedinfo); $lat = $geocodedinfo->results[0]->geometry->location->lat; $lng = $geocodedinfo->results[0]->geometry->location->lng; $query = "UPDATE location SET loc_address='$addr1', loc_address2='$addr2', loc_city='$city', loc_state='$state',loc_zip='$zip', loc_country='$sn', lat='$lat',lng='$lng' WHERE loc_id='$locid'"; $result = mysqli_query($query); if ($result){ echo "<h2><b><strong>OMG it worked!</strong></b></h2>"; }else { echo "<p>No geocode (mope)" . mysqli_error() . "</p>"; } can I force the insert submit to go first and then run the update? if so, what do you suggest? I came across one article that said something about making each query into a function, but it wasn't explained very well. And do I need to create a separate pre-populated hidden form somehow for the second submit?
-
Hello, I am trying to follow along in the book. I am on page 135 number 2. "Insert One row of data into the users table WITHOUT naming the columns.Because I chose to use PHPmyadmin, I don't see how to do this. There is no example in the book. My instructor emailed me and said to use SQL Tab with Insert.. When I do this, I see the names of the columns in there.. Do I delete these? Not sure how to do this excersize with PHPmyadmin