Szabi Zsoldos 26 Learner and helper guy

You can update all directly from MySQL

mysql -> UPDATE yourTable SET ON yourField='newLink' WHERE yourField='oldLink'
Szabi Zsoldos 26 Learner and helper guy

No tested, but something like this.

<?php

    $now = NOW();
    
    // fetch the date from the database
    
    $yourTimeVariable =  date("Y-M-D-H:m",strtotime("+1 day"));    
    
    if (($_SERVER['REMOTE_ADDR'] == $yourDatabaseIP) && ($now == $yourTimeVariable)) {
        echo 'You have already voted, please come back in 24h.';
    } 
    else {
        // voting script
    }
    
?>
Szabi Zsoldos 26 Learner and helper guy

Hello,

What I'm intending to do it's an Organization chart with departments and members.

Basically an organigram with different departments and different members.


This is the MySQL database.
And here is my attempt of doing this horizontally, but i`m kind of stuck.
Could you give me some hints on how to do this ? Thank you!

# --------------------------------------------------------
# Host:                         127.0.0.1
# Database:                     materom
# Server version:               5.1.41
# Server OS:                    Win32
# HeidiSQL version:             5.0.0.3272
# Date/time:                    2010-11-27 12:23:36
# --------------------------------------------------------

# Dumping structure for table materom.organigrama
CREATE TABLE IF NOT EXISTS `organigrama` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `nume` varchar(50) DEFAULT NULL,
  `nivel` int(10) DEFAULT NULL,
  `id_cont` int(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

# Dumping data for table materom.organigrama: 7 rows
INSERT INTO `organigrama` (`id`, `nume`, `nivel`, `id_cont`) VALUES (1, 'Szabol Zsoldos', 4, 244), (2, 'Rodica Baciu', 1, 2), (3, 'Matei Baciu', 1, 1), (4, 'Beniamin Suciu', 2, 3), (6, 'Rezso Zsogmond', 3, 247), (5, 'Corinta Contiu', 2, 266), (8, 'Dan Milascon', 4, 248);

/////////// PHP + HTML

<?php
    include('db.php');
    
    $q1 = "SELECT * FROM materom.organigrama AS o INNER JOIN materom.contact AS m ON o.id_cont = m.id WHERE o.nivel = '1' ORDER BY o.nivel";
    $result = mysql_query($q1);

    $q2 = "SELECT * FROM materom.organigrama AS o INNER JOIN materom.contact AS m ON o.id_cont = m.id WHERE o.nivel = '2' ORDER BY o.nivel";
    $result2 = mysql_query($q2); 

    $q3 = "SELECT * FROM materom.organigrama AS o INNER JOIN materom.contact AS …
Szabi Zsoldos 26 Learner and helper guy

here I have

//functions.php

<?php
session_start();

function loggedin(){
        if(@$_SESSION['username']){
            @$username = $_SESSION['username'];
            return true;
    } elseif(@$_COOKIE['username']) {
        @$username = $_COOKIE['username'];
        return true;
        }else {
            return false;
        }
}

function getuid($username) {
    $res = mysql_query("SELECT id FROM users WHERE username='$username'");
    if(mysql_num_rows($res)) {
        $row = mysql_fetch_assoc($res);
        return $row['id'];
    }
}

function getusername($uid){
    $res = mysql_query("SELECT username FROM users WHERE id='$uid'");
    if(mysql_num_rows($res)) {
       $row = mysql_fetch_assoc($res);
       return $row['username']; 
    }
}

function getsig($uid) {
    $res = mysql_query("SELECT signature FROM users_extra WHERE id='$uid'") or die(mysql_error($res));
    if(mysql_num_rows($res)) {
        $row = mysql_fetch_assoc($res);
        return $row['signature'];
    }
}


function getprofile($uid) {
    $res = mysql_query("SELECT profile FROM users_extra WHERE id='$uid'") or die(mysql_error($res));
    $row = mysql_fetch_assoc($res);
    return $row['profile'];
}
?>

When I try to call

getsig();

and

getprofile();

on the user.php it wont show up, and I cant figure it out why.
The functions seems to be allright, what can be the problem ?

//user.php

<?php
    if(@$_SESSION['username']){
            @$username = $_SESSION['username'];
    } elseif(@$_COOKIE['username']) {
        @$username = $_COOKIE['username'];
      }
      
    if($_GET['id']) {
        $uid = getuid($username);
        $username2 = getusername($_GET['id']);
        if($username2 == "") {
            echo "User does not exist.";
            exit();
        } else {
           ?>
           
           <table width="100%">
            <tr valign="top">
                <td width="10%">
                    <fieldset>
                    <legend><?php echo "Logged in as, <a href=\"index.php?act=profile&id=".$uid."\">$username2</a>"; ?></a></legend> 
                    </fieldset>
                </td>
                
                <td width="77%">
                    <fieldset>
                    <legend>Signature</legend>
                    <form action="./includes/update.php" method="post">
                    <textarea style="height: 100px;" name="ud_sig"></textarea>
                    <input type="hidden" name="ud_id" value='<? echo "$uid";?>'/>
                    </br>
                    <input type="submit" value="Add Signature" name="signature"/>
                    </form>
                    //here is problem #1
                    <?php echo getsig(getuid($username2)); ?>
                    </fieldset>
                    
                    <fieldset>
                    <legend>Profile</legend>
                    //here is problem #2
                    <?php echo getprofile(getuid($username2)); ?>
                    </fieldset>
                </td>
                
                <td width="8%">
                    <fieldset>
                    <legend>User info</legend>
                    <p>Posts: 0</p>                  
                    </fieldset>
                </td> …
Szabi Zsoldos 26 Learner and helper guy

Thank you for the explanation!

Szabi Zsoldos 26 Learner and helper guy

Thank you ardav,

What do you mean by escaping integers as well ? I escaped the password and the username, what else should i escape ?

Szabi Zsoldos 26 Learner and helper guy

Hey guys,

Could you help me understand how to implement

mysql_real_escape_string

to prevent injection in this code ?

<?php

    if(loggedin()){
        echo "You are already logged in.";
    } else {
        if($_POST['submit']){
            if($_POST['username'] && $_POST['password']) {
                $username = $_POST['username'];
                $password = $_POST['password'];
                $password = md5($password);
                $res = mysql_query("SELECT * FROM users WHERE username='$username'");
                if(mysql_num_rows($res) == 0){
                    echo "Utilizator inexistent, click <a href='index.php?act=register'>aici</a> pentru inregistrare.";
                    exit();
                }
                $row = mysql_fetch_assoc($res);
                if($row['password'] == $password) {
                    if(@$_POST['remember' == "on"]) {
                        setcookie('username', $username,time()+36000);
                        echo "You have been logged in,<a href='index.php'>click here</a> to continue.";
                    } else {
                        $_SESSION['username'] = $username;
                        echo "You have been logged in,<a href='index.php'>click here</a> to continue.";
                    }
                } else {
                    echo "Incorrect password.";
                }
            }
        } else {
            
      
?>
Szabi Zsoldos 26 Learner and helper guy

Got it working, I couldnt connect tot de DB ! sorry for the confusion :)

Szabi Zsoldos 26 Learner and helper guy

The problem is that I haven't used PHP from 2008 and i had this old file which worked and now it's giving me the same fetch problem.

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\XAMPP\xampp\htdocs\tabara\inscrisi\index.php on line 40

I tried your previous recommendations but the same issue.

What should i do to work ?

<html>
<title>BLA</title>
<head>

<style type="text/css" media="all">

@import url(/rev/css.css);

</style>
</head>
<body>
<table align="center" id="table1" width="900" height="300" align="center" style="border-bottom-style:solid; border:solid; border-color:#333">
<tr>
<td>
<div align="center">
<?php

	$con = mysql_connect("localhost","root","");
		if (!$con){
		  die('Could not connect: ' . mysql_error());
		}

	mysql_select_db("localhost", $con);

	$result = mysql_query("SELECT * FROM inscrisi");

	echo "<table style='border-bottom-style:solid; border:solid; border-color:#333333'>
<br>
<tr>
<th width='30px' style='background-color:#414141; color:#FFCC00'>ID</th>
<th width='100px' style='background-color:#414141; color:#FFCC00'>Numele</th>
<th width='100px' style='background-color:#414141; color:#FFCC00'>Prenumele</th>
</tr>";

	mysql_select_db("localhost", $con);


	$result = mysql_query("SELECT * FROM inscrisi ORDER BY nume ASC, varsta DESC");

	while($row = mysql_fetch_assoc($result))
	  {
	  echo "<tr style='border:solid; border:1px; border-color:#FFFFFF'>";
	  echo "<td align='center'>" . $row['id'] . "</td>";
	  echo "<th align='left' style=' color:#1186DD '>" . $row['nume'] . "</th>";
	  echo "<td align='center'>" . $row['prenume'] . "</td>";
	  echo "</tr>";
	  }
	echo "</table>";

	mysql_close($con);


?>
</div>

</td>
</tr>
</table>
</body>
</html>
Szabi Zsoldos 26 Learner and helper guy

Hey guys,

I know that the query is not good, but i CANT see the problem.

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\XAMPP\xampp\htdocs\betania\modify.php on line 8

Could you please help me out?

<?php

	include "connection.php";
	
	if(!isset($_POST[submit])) {
		$q = mysql_query("SELECT * FROM inscrieri  WHERE id =".$_GET['id']."");
		$result = mysql_query($q);
		$person = mysql_fetch_array($result, MYSQL_ASSOC);
		
		
	}	else {
	
	}
		

?>

<html>
		<head>
			<title>Modificare participant - Lunca Bradului 2010</title>
		<style type="text/css">
		
		#loginform {
			border: 2px solid #500;
			background-color: #90A4CA;
			width: 300px;
			text-align: left;
		}
		
		#loginform form {
			margin: 5px;
		}
		
		#loginform label {
			width: 100px;
			float: left;
			display: block;
			clear: both;
		}
		
		#loginform label, input { 
			margin-bottom: 5px;
		}
		
		#loginform input {
			background-color: #FFCC00;
			border: 1px solid #CCC;
		}
		
		</style>
		
		</head>
		
	<body>
	
		<center>
		<div id="loginform">
			<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
				<label>Nume:</label>
					<input type="text" name="inputNume" id="nume" value="<?php echo  $person[nume]; ?>">
				<label>Prenume:</label>
					<input type="text" name="inputPrenume" id="prenume" value="<?php echo  $person[prenume]; ?>">
				<label>Telefon:</label>
					<input type="text" name="inputTelefon" id="telefon" value="<?php echo  $person[telefon]; ?>">
				<label>Email:</label>
					<input type="text" name="inputMail" id="mail" value="<?php echo  $person[mail]; ?>">
				<label>Varsta:</label>				
					<input type="text" name="inputVarsta" id="varsta" value="<?php echo  $person[varsta]; ?>">
				
				<input type="hidden" name="id" value="<?php $_GET[id]; ?>"
				<input type="submit" value="Modifica"/>
			</form>
		</div>
		</center>
	
	
	
	</body>
</html>

<?php 
	if(isset($_POST['submit'])) {
		$update = "UPDATE inscrieri SET nume='$_POST[inputNume]', prenume='$_POST[inputPrenume]', telefon='$_POST[inputTelefon]', mail='$_POST[inputMail]', varsta='$_POST[inputVarsta]' WHERE id = $_POST[id]";
		mysql_query($update) or die (mysql_error());
		
		echo "Participantul a fost modificat!";
		header("Location: index.php");
	} else {
	
	}
?>
Szabi Zsoldos 26 Learner and helper guy

Thanks, my next question is, that how can assign numbers from srand to my questions?

int i;
    srand(time(NULL));
    
    i = rand();
Szabi Zsoldos 26 Learner and helper guy

Hey, this is my first post here, glad to be here and learn from you guys!

what i'm trying to do is this:

I would like to enter 2 names and then hit a number from lets say 1 to 10, and i have the array Q1,Q2... etc. these should be the questions. I want to shuffle them and if I press every time the same number it should give me a another question for another name.

How can I do this? I saw this kind of app on the iphone, and it was fun! But since I'm new to C++, i need some kind of help.
Thank you!

#include <stdlib.h>
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    const int QA = 10;
    string  question[QA] = {"Q1", "Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9", "Q10", };
    string name1, name2;
    int choice;

    cout << "Enter Name1: ";
    cin >> name1;
    cout << "Enter Name2: ";
    cin >> name2;
    

    system("PAUSE");
    
}
Szabi Zsoldos 26 Learner and helper guy

Got it working, thanks!

Szabi Zsoldos 26 Learner and helper guy

Yes, I want to multiply the next input, but it doesn't clear my memory so next time it will multiply with my last result. (The sum remains in the cache I think and I would like to empty it without exiting the program)

Szabi Zsoldos 26 Learner and helper guy

Now ... everything is working fine, but I have some problems, clearing the stream, after an operation.

5*5 = 25

Next time it will 25*n ... I dont know how to do that.

#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>

using namespace std;

int main()
{   
    string my_array[5];
    int enter;
    int elements[255];
    int nr_elem;
    int quantity, value;
    double vat = 1.19;
    int i = 0;
    int j = 0;
    int sum = 0;
    int multi = 1;
    int div_a, div_b;
    int divide;
    int a[256];
    int loop = 1;

    while (loop == 1)
    {
    
    cout << "1. Add / Sum" << endl;
    cout << "2. Multiplication" << endl;    
    cout << "3. Divide" << endl;    
    cout << "4. Modulo" << endl;        
    cout << "5. VAT" << endl << endl;            
     
    cout << "Chose 1-5 (9 to exit): ";
    cin >> enter;
    
    cout.flush();
    
    
    switch(enter)    
    {
                     
                     case 1:
                                                    
                          system("CLS");
                          
                          cout << "Input nr. of elemets: ";
                          cin >> nr_elem;

                          for (int i=0; i < nr_elem; i++)
                          {
                              cout << "Element [" << i << "]=";
                              cin >> elements[i];
                              sum += elements[i];
                          
                          }
                              cout<<"Sum of elements: "<< sum << endl;
                                                                                      
                          system("PAUSE");
                          system("CLS");
                          
                          
                     break;
                     
                     case 2:
                          
                          
                          system("CLS");
                          
                          cout << "Input nr. of elements: ";
                          cin >> nr_elem;
                          
                          for (int i=1; i <= nr_elem; i++)
                          {
                              cout << "Element [" << i << "]= ";
                              cin >> elements[i];
                              multi *= elements[i];
                          }
                          
                          cout << "Sum of multiplication: " << multi  << endl;
                          
                          system("PAUSE");
                          system("CLS");
                          
                          
                     break;
                     
                     
                     
                     
                     
                     case 3:
                          
                          system("CLS");
                          
                              cout << "Enter two elements " …
Szabi Zsoldos 26 Learner and helper guy

Got it working, thank you !

like this

case 1:
                                                    
                          system("CLS");
                          
                          cout << "Input nr. of elemets: ";
                          cin >> nr_elem;

                          for (int i=0; i < nr_elem; i++)
                          {
                              cout << "Element [" << i << "]=";
                              cin >> elements[i];
                              sum += elements[i];
                          
                          }
                              cout<<"Sum of elements: "<< sum << endl;
                                                                                      
                          system("PAUSE");
                          system("CLS");

                          
                          
                     break;

now when I try to use *= it always outputs me 0, why?

Szabi Zsoldos 26 Learner and helper guy

Oh common, I'm not ethnocentric, not even angry as you assume, I am learning C++ by small steps, and I thought that I can enter the values into the arrays. I know that it can be done with accumulator, but I was trying something different.

Thank you for your patience anyway!

Szabi Zsoldos 26 Learner and helper guy

So here it is in english.

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
    string my_array[5];
    int enter;
    int elements = 0 , elements_added = 0;
    int cantitate, valoare;
    double tva = 1.19;
    int i = 0;
    int j = 0;
    int a[256];
    int loop = 1;

    while (loop == 1)
    {
    
    cout << "1. Add / Sum" << endl;
    cout << "2. Multiplication" << endl;    
    cout << "3. Divide" << endl;    
    cout << "4. Modulo" << endl;        
    cout << "5. VAT" << endl << endl;            
     
    cout << "Chose 1-5 (9 to exit): ";
    cin >> enter;
    
    switch(enter)    
    {
                     
                     case 1:
                          
                          cout << "Input nr. of elemets: ";
                          cin >> elements;
                          
                          for ( j=1; j<=i; j++)
                          {
                              cout << "Element [" << j << "]=";                                        
                              cin >> elements_added;
                              i++;
                              a[i] = elements_added;
                          }


                          
                          
                     break;
                     
                     case 2:
                          
                     break;
                     
                     case 3:
                          
                     break;
                     
                     case 4:
                          
                     break;  
                     
                     case 5:
                          
                          system("CLS");
                          
                          cout << "Quantity: ";
                          cin >> cantitate;
                          
                          cout << "Valoare BRUT: ";
                          cin >> valoare;
                          
                          cout << "Valoare totatala BRUT: " << cantitate * valoare << endl;
                          cout << "Valoarea TVA: " <<(valoare * cantitate * tva) - valoare * cantitate << endl;
                          cout << "Valoarea totala TVA: " << valoare * cantitate * tva << endl;
                          
                          system("PAUSE");
                          system("CLS");
                          
                     break;    
                     
                     case 9:
                          exit(0);
                          break;


    }
    }    
    
    
    system ("PAUSE");
    return 0;
}
Szabi Zsoldos 26 Learner and helper guy

I would like that case 1: should display how many elements do i want, and that i can assign values to those elements. Mine because of the j++ goes infinite, so i can enter infinite values to infinite arrays.

How could i do this?

Cate elemente vrei sa adaugi: 3

Elementul[1] = 2
Elementul[2] = 2314
Elementul[3] = 635643

after entering the third value, it should display the sum(i can do that, but i`m having difficulties making the inputs from the "cin")

while (loop == 1)
    {
    
    cout << "1. Adaugare" << endl;
    cout << "2. Multiplicare" << endl;    
    cout << "3. Impartire" << endl;    
    cout << "4. Modulo" << endl;        
    cout << "5. TVA" << endl << endl;            
     
    cout << "Alege 1-5 (9 to exit): ";
    cin >> enter;
    
    switch(enter)    
    {
                     
                     case 1:
                          
                          cout << "Cate elemente vrei sa adaugi: ";
                          cin >> elements;
                          
                          for ( j=1; j<=i; j++)
                          {
                              cout << "Elementul [" << j << "]=";                                        
                              cin >> elements_added;
                              i++;
                              a[i] = elements_added;
                          }


                          
                          
                     break;
Szabi Zsoldos 26 Learner and helper guy

My question is the following, i'm just figuring out this language, now my question is about the "n" number.

What is the role of n<5, i know after this condition it continues the for or not.
But i can't find the precise explanation of the "n" . Thank you.

Why are n<5 and n<6 outputting the same result? And why n<4 is so small?

n<4 outputs 135
n<5 outputs 12206
n<6 outputs 12206
n<7 outputs 12207
n<8 outputs 12208

#include <stdafx.h>
#include <iostream>

using namespace std;

int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;

int main ()
{
  for ( n=0 ; n<5 ; n++ )
  {
    result += billy[n];
  }
  cout << result << endl;
  return 0;
}
Szabi Zsoldos 26 Learner and helper guy

It was a php matter, I found the solution.

*almostbob - Thank you, this is a much better way, thank you :)

Szabi Zsoldos 26 Learner and helper guy

Hi, this is my first post here,
I would like to create x fields with name email age sex and the subscribe question, this is just an example page.

If i would select the number 5,4, or any number then i would like to see 5 fields with names and emails, etc.

What should I do ? It's a php matter ?

Thank you,
Szabi Zs.

<html>
<head>
<title>option</title>

</head>
<body>
<table>
<tr>
		<td>
        
<form action="" method="get">

<p> How many people? </p>
    <select name="select" class="form">
    	<option value="1">1</option>
    	<option value="2">2</option>        
    	<option value="3">3</option>   
        <option value="4">4</option>
        <option value="5">5</option>     
    </select>
    
<br /><br />	   
name:<br /><input type="text" name="name" class="form"/>
<br />
email:<br /><input type="text" name="mail" class="form"/>
<br />
age:<br /><input type="text" name="age" class="form" maxlength="2" />
<br /><br />
<div>
Sex: M<input name="sex" type="radio"  value="male"> F<input name="sex" type="radio" value="female" />
</div>
<br /><br />
<div>
<p class=" p2">Would you like to be informed all the time there is something new? By this we add you in our database: </p>
Yes<input name="inform" type="radio"  value="yes"> No<input name="inform" type="radio" value="no" />
</div>
<br />
<div align="right">
<input type="submit" value="Subscribe »" onClick="return verify();" class="bt">
</div>
    
</form>
        
        
		</td>
</tr>
</table>
</body>
</html>