Solved

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

Posted on 2007-03-24
4
263 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now