Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Updating a table after having run Select on the very same table and 2massaged" the data.

Posted on 2007-03-24
4
Medium Priority
?
287 Views
Last Modified: 2013-12-13
Having this code:
if($_POST['medlemsnr_1'] == "alla" && empty($_POST['medlemsnr_2']) && empty($_POST['medlemsnr_3']) && empty($_POST['medlemsnr_4']) && empty($_POST['medlemsnr_5']) && $_POST['medlemsnr_6'] == "Lägsta nr" && $_POST['medlemsnr_7'] == "Högsta nr")
{ $SQL = " SELECT * FROM medlem "; }

// Om bara "Har hamnplats" är markerat
if(!empty($_POST['medlemsnr_2']) && $_POST['medlemsnr_2'] == "hamn" && empty($_POST['medlemsnr_1']) && empty($_POST['medlemsnr_3']) && empty($_POST['medlemsnr_4']) && empty($_POST['medlemsnr_5']) && $_POST['medlemsnr_6'] == "Lägsta nr" && $_POST['medlemsnr_7'] == "Högsta nr")
{ $SQL = " SELECT * FROM medlem WHERE brygga IS NOT NULL"; }

// Om bara "Har varvsplats" är markerat
if(!empty($_POST['medlemsnr_3']) && $_POST['medlemsnr_3'] == "varv" && empty($_POST['medlemsnr_1']) && empty($_POST['medlemsnr_2']) && empty($_POST['medlemsnr_4']) && empty($_POST['medlemsnr_5']) && $_POST['medlemsnr_6'] == "Lägsta nr" && $_POST['medlemsnr_7'] == "Högsta nr")
{ $SQL = " SELECT * FROM medlem WHERE omrade IS NOT NULL"; }

//Om bara "Lägsta nummer" valts med comboboxen
if(!empty($_POST['medlemsnr_4']) && $_POST['medlemsnr_4'] > 0 && empty($_POST['medlemsnr_1']) && empty($_POST['medlemsnr_2']) && empty($_POST['medlemsnr_3']) && empty($_POST['medlemsnr_5']) && $_POST['medlemsnr_6'] == "Lägsta nr" && $_POST['medlemsnr_7'] == "Högsta nr")
{ $SQL = " SELECT * FROM medlem WHERE medlemsnr >= '$_POST[medlemsnr_4]'"; }

//Om bara "Högsta nummer" valts med comboboxen
if(!empty($_POST['medlemsnr_5']) && $_POST['medlemsnr_5'] > 0 && empty($_POST['medlemsnr_1']) && empty($_POST['medlemsnr_2']) && empty($_POST['medlemsnr_3']) && empty($_POST['medlemsnr_4']) && $_POST['medlemsnr_6'] == "Lägsta nr" && $_POST['medlemsnr_7'] == "Högsta nr")
{ $SQL = " SELECT * FROM medlem WHERE medlemsnr <= '$_POST[medlemsnr_5]'"; }

//Om bara "Lägsta nummer" valts i formulärrutan (inte comboboxen)
if($_POST['medlemsnr_6'] !== "Lägsta nr" && $_POST['medlemsnr_6'] > 0 && empty($_POST['medlemsnr_1']) && empty($_POST['medlemsnr_2']) && empty($_POST['medlemsnr_3']) && empty($_POST['medlemsnr_4']) && empty($_POST['medlemsnr_5']) && $_POST['medlemsnr_7'] == "Högsta nr")
{ $SQL = " SELECT * FROM medlem WHERE medlemsnr >= '$_POST[medlemsnr_6]'"; }

//Om bara "Högsta nummer" valts i formulärrutan (inte comboboxen)
if($_POST['medlemsnr_7'] !== "Högsta nr" && $_POST['medlemsnr_7'] > 0 && empty($_POST['medlemsnr_1']) && empty($_POST['medlemsnr_2']) && empty($_POST['medlemsnr_3']) && empty($_POST['medlemsnr_4']) && empty($_POST['medlemsnr_5']) && $_POST['medlemsnr_6'] == "Lägsta nr")
{ $SQL = " SELECT * FROM medlem WHERE medlemsnr <= '$_POST[medlemsnr_7]'"; }

// Om både "Lägsta nummer" och "Högsta nummer" valts i comboboxen
if(!empty($_POST['medlemsnr_4']) && $_POST['medlemsnr_4'] > 0 && !empty($_POST['medlemsnr_5']) && $_POST['medlemsnr_5'] >0 && empty($_POST['medlemsnr_1']) && empty($_POST['medlemsnr_2']) && empty($_POST['medlemsnr_3']) && $_POST['medlemsnr_6'] == "Lägsta nr" && $_POST['medlemsnr_7'] == "Högsta nr" )
{ $SQL = " SELECT * FROM medlem WHERE medlemsnr >= '$_POST[medlemsnr_4]' AND medlemsnr <= '$_POST[medlemsnr_5]' "; }

// Om både "Lägsta nummer" och "Högsta nummer" valts i formulärrutorna (inte comboboxarna)
if($_POST['medlemsnr_6'] !== "Lägsta nr" && $_POST['medlemsnr_6'] > 0 && $_POST['medlemsnr_7'] !== "Högsta nr" && $_POST['medlemsnr_7'] >0 && empty($_POST['medlemsnr_1']) && empty($_POST['medlemsnr_2']) && empty($_POST['medlemsnr_3']) && empty($_POST['medlemsnr_4']) && empty($_POST['medlemsnr_5']) )
{ $SQL = " SELECT * FROM medlem WHERE medlemsnr >= '$_POST[medlemsnr_6]' AND medlemsnr <= '$_POST[medlemsnr_7]' "; }

// Om "Eller endast"  valts i formulärrutan (inte comboboxarna)
if($_POST['medlemsnr_8'] !== "" && $_POST['medlemsnr_8'] > 0 && $_POST['medlemsnr_7'] == "Högsta nr" && $_POST['medlemsnr_6'] == "Lägsta nr" && empty($_POST['medlemsnr_1']) && empty($_POST['medlemsnr_2']) && empty($_POST['medlemsnr_3']) && empty($_POST['medlemsnr_4']) && empty($_POST['medlemsnr_5']) )
{ $SQL = " SELECT * FROM medlem WHERE medlemsnr = '$_POST[medlemsnr_8]' "; }

//echo"<br>SQL: $SQL";

$ret = mysql_db_query($db, $SQL, $cid);
if (!$ret) { echo( mysql_error()); }
else {
while ($row = mysql_fetch_array($ret)) {   // This is line 89
            $id  = $row["id"];
            $brygg_bredd  = $row["brygg_bredd"];
            $bat_bredd  = $row["bat_bredd"];
            $bat_langd  = $row["bat_langd"];
            $medlemsnr  = $row["medlemsnr"];
            $fornamn  = $row["fornamn"];
            $efternamn  = $row["efternamn"];
            $adress  = $row["adress"];
            $postnr  = $row["postnr"];
            $ort  = $row["ort"];
            $betalt = $row["betalt"];
            $typ  = $row["typ"];
            $brygga  = $row["brygga"];
            $bat_vikt  = $row["bat_vikt"];

                             ----------------------------------------------------------------
and after having "massaged" the data I get from the database, I'd like to run this code:
                             ----------------------------------------------------------------

@mysql_close();
@mysql_connect($host,$usr,$pwd)
or die("<p>Kan ej ansluta till databasservern.</p>");
@mysql_select_db($db)
or die("<p>Kan ej ansluta till databasen " . $db . ".</p>");
$SQL = " UPDATE medlem SET faktura_nr = '$fakturanr',  faktura_summa = '$totalt', faktura_datum = curdate()  WHERE medlemsnr = '$medlemsnr'  ";    
$ret = mysql_db_query($db, $SQL, $cid);  
if (!$ret) { echo( mysql_error()); }

I get this message from MySQL when running the script:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Mina dokument\htdocs\tbk\kansli\faktura.php on line 89.

Can anyone of you gurus, please, help me and explain what is wrong and how I could correct it.
0
Comment
Question by:lericson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 18786962
I don't think you need to close your database connection. Remove:

@mysql_close();
@mysql_connect($host,$usr,$pwd)
or die("<p>Kan ej ansluta till databasservern.</p>");
@mysql_select_db($db)
or die("<p>Kan ej ansluta till databasen " . $db . ".</p>");

and you should be fine, since you are already connected to the database.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 18787739
You'd probably need to use:

@mysql_close();
$db = @mysql_connect($host,$usr,$pwd)  // notice $db = in this line <----
or die("<p>Kan ej ansluta till databasservern.</p>");
@mysql_select_db($db)
or die("<p>Kan ej ansluta till databasen " . $db . ".</p>");

-r-
0
 

Author Comment

by:lericson
ID: 18788670
Thanks Zyloch and Roonaan!
If I follow Zyloch's suggestion, I get: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Mina dokument\htdocs\tbk\kansli\faktura.php on line 89
If I follow Roonaan's suggestion, I get: Cannot connect to database Resource id #5.
What should I do?
0
 

Accepted Solution

by:
lericson earned 0 total points
ID: 18788739
It works very well when some of the variables got renamed:
$SQL3 = " UPDATE medlem SET faktura_nr = '$fakturanr',  faktura_summa = '$totalt', faktura_datum = curdate()  WHERE medlemsnr = '$medlemsnr'  ";    
$ret3 = mysql_db_query($db, $SQL3, $cid);  
if (!$ret3) { echo( mysql_error()); }
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question