Jump to content

Can Anyone Help Me with this Script


nepzap2

Recommended Posts

I'm trying to replace an image already entered in a file along with information already entered in a MySQL Database. But I can get this script to work.

 

<?
	  include("conn.php");
	  $mode=$_GET["mode"];
	  if($mode=="add") {
	  	$first_name=$_POST["first_name"];
		$last_name=$_POST["last_name"];
		$title=$_POST["title"];
		$work_group=$_POST["work_group"];
		$email=$_POST["email"];

		$sql="insert into staff_contact (first_name,
										last_name,
										title,
										work_group,
										email) 
						  values('$first_name',
								 '$last_name',
								 '$title',
								 '$work_group',
								 '$email')";

		$result=mysql_query($sql,$conn) or die(mysql_error());
		header("location: ../viewNews.php");

	  } elseif($mode=="update") {
	  	$id=$_POST["id"];
		$title=$_POST["title"];
		$date_time=$_POST["date_time"];
		$category=$_POST["category"];
		$speaker=$_POST["speaker"];
		$room=$_POST["room"];
		$description=$_POST["description"];


		$image="<img src='../saimages/" . $row['id'] . "' height='100' width='90'>";

		$sql="update staff_contact set id='$id',
									   title='$title',
									   date_time='$date_time',
									   category='$category',
									   speaker='$speaker',
									   room='$room',
									   description='$description'where id='$id'";


		$result=mysql_query($sql,$conn) or die(mysql_error());




		////This is the extra code I added
		if($result){
          
             $event_id = mysql_insert_id();
             $uploadfile = "../../saimages/$event_id";
		 move_uploaded_file($_FILES['image_data']['tmp_name'], $uploadfile);
		 header("location: ../viewNews.php");             

	  	}
	  //End of code I added





	  }
?>

 

Any help is greatly appreciated

Link to comment
https://forums.phpfreaks.com/topic/135220-can-anyone-help-me-with-this-script/
Share on other sites

I think you're missing the image extension;

 

<?php
$event_id = mysql_insert_id();
$uploadfile = "../../saimages/$event_id";
?>

 

try using this if its a jpg;

 

<?php
$event_id = mysql_insert_id();
$uploadfile = "../../saimages/$event_id.jpg"; //that is if the file exists
?>

Thank you very Much gevans but I don't think that is the case.

 

 

This is the form

 


<form enctype="multipart/form-data" method="post" action="CMS2/newsAdd.php">
            <table>
            	
             <?php if(isset($_SESSION['title_error'])){ 
                      $title_error = $_SESSION['title_error'];
                      print '<tr><td colspan="2"><font color="red">' . $title_error. '</font></td></tr>';
                      unset($_SESSION['title_error']);
                   } 
             ?>
            
             <tr>
              <td>Title:</td>
              <td colspan="6"><input type="text" name="title" size="50" value="<?php if(isset($_SESSION['title'])){ print $_SESSION['title']; unset($_SESSION['title']);} ?>"></td>
             </tr>
            
             <?php if(isset($_SESSION['datetime_error'])){ 
                      $datetime_error = $_SESSION['datetime_error'];
                      print '<tr><td colspan="2"><font color="red">' . $datetime_error . '</font></td></tr>';
                      unset($_SESSION['datetime_error']);
                   } 
             ?>
            
            
             <tr>
              <td valign="bottom">Date: <input type="text" id="popup_container" name="date" value="<?php if(isset($_SESSION['date'])){ print $_SESSION['date']; unset($_SESSION['date']);} ?>" size="10" /> <a href="#" onClick="dp_cal.show();"></a></td>
              <td>Time:
               <select name="hour">
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
                <option>6</option>
                <option>7</option>
                <option>8</option>
                <option>9</option>
                <option>10</option>
                <option>11</option>
                <option>12</option>
               </select>:
               <select name="minute">
                <option>0</option>
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
                <option>6</option>
                <option>7</option>
                <option>8</option>
                <option>9</option>
                <option>10</option>
                <option>11</option>
                <option>12</option>
                <option>13</option>
                <option>14</option>
                <option>15</option>
                <option>16</option>
                <option>17</option>
                <option>18</option>
                <option>19</option>
                <option>20</option>
                <option>21</option>
                <option>22</option>
                <option>23</option>
                <option>24</option>
                <option>25</option>
                <option>26</option>
                <option>27</option>
                <option>28</option>
                <option>29</option>
                <option>30</option>
                <option>31</option>
                <option>32</option>
                <option>33</option>
                <option>34</option>
                <option>35</option>
                <option>36</option>
                <option>37</option>
                <option>38</option>
                <option>39</option>
                <option>40</option>
                <option>41</option>
                <option>42</option>
                <option>43</option>
                <option>44</option>
                <option>45</option>
                <option>46</option>
                <option>47</option>
                <option>48</option>
                <option>49</option>
                <option>50</option>
                <option>51</option>
                <option>52</option>
                <option>53</option>
                <option>54</option>
                <option>55</option>
                <option>56</option>
                <option>57</option>
                <option>58</option>
                <option>59</option>
               </select>
               <select name="am_pm">
                <option>AM</option>
                <option>PM</option>
               </select>
              </td>
             </tr>
            
             <tr>
              <td>Category:</td>
              <td>
               <select name="category">
                <option selected>BTI Scientist</option>
                <option>Distinguished lecturer</option>
                <option>Professional development series</option>
                <option>Special Guest Speaker</option>
               </select>
              </td>
             </tr>
            
             <?php if(isset($_SESSION['speaker_error'])){ 
                      $speaker_error = $_SESSION['speaker_error'];
                      print '<tr><td colspan="2"><font color="red">' . $speaker_error . '</font></td></tr>';
                      unset($_SESSION['speaker_error']);
                   } 
             ?>
            
             <tr>
              <td>Speaker:</td>
              <td><input type="text" name="speaker" size="50" value="<?php if(isset($_SESSION['speaker'])){ print $_SESSION['speaker']; unset($_SESSION['speaker']);} ?>" ></td>
             </tr>
            
             <?php if(isset($_SESSION['room_error'])){ 
                      $room_error = $_SESSION['room_error'];
                      print '<tr><td colspan="2"><font color="red">' . $room_error . '</font></td></tr>';
                      unset($_SESSION['room_error']);
                   } 
             ?>
            
             <tr>
              <td>Room:</td>
              <td><input type="text" name="room" value="<?php if(isset($_SESSION['room'])){ print $_SESSION['room']; unset($_SESSION['room']);} ?>" ></td>
             </tr>
            
             <?php if(isset($_SESSION['desc_error'])){ 
                      $desc_error = $_SESSION['desc_error'];
                      print '<tr><td colspan="2"><font color="red">' . $desc_error . '</font></td></tr>';
                      unset($_SESSION['desc_error']);
                   } 
             ?>
            
             <tr>
              <td valign="top">Description:</td>
              <td><textarea name="desc" cols="50" rows="10" wrap="soft"><?php if(isset($_SESSION['desc'])){ print $_SESSION['desc']; unset($_SESSION['desc']);} ?></textarea></td>
             </tr>
            
             <tr>
              <td> 
               Image File:
              </td>
              <td>
               <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />  <!-- image max size is 2 MB = 1024 * 1024 * 2 bytes --> 
               <input name="image_data" type="file" />
              </td>
             </tr>
            
             <tr>
              <td colspan="2" align="center" bgcolor="#eeeeee"><input type="submit" value="Add Event"></td>
             </tr>
            </table>
            </form>

 

 

This is the code that handles putting the data in the MySQL Database

 


<?php
    session_start();
    include_once('conn.php');

    $error = false;

    if(empty($_POST['title']) || "" === trim($_POST['title'])){
        $error = true;
        $_SESSION['title_error'] = "Please provide a title for this event.";
    }
    else{ $_SESSION['title'] = $_POST['title']; }

    if(empty($_POST['date']) || "" === trim($_POST['date'])){
        $error = true;
        $_SESSION['datetime_error'] = "Please select a date for this event.";
    }
    else{ $_SESSION['date'] = $_POST['date']; }

    if(empty($_POST['speaker']) || "" === trim($_POST['speaker'])){
        $error = true;
        $_SESSION['speaker_error'] = "Please provide a speaker for this event.";
    }
    else{ $_SESSION['speaker'] = $_POST['speaker']; }

    if(empty($_POST['room']) || "" === trim($_POST['room'])){
        $error = true;
        $_SESSION['room_error'] = "Please provide a room for this event.";
    }
    else{ $_SESSION['room'] = $_POST['room']; }

    if(empty($_POST['desc']) || "" === trim($_POST['desc'])){
        $error = true;
        $_SESSION['desc_error'] = "Please provide a description for this event.";
    }
    else{ $_SESSION['desc'] = $_POST['desc']; }


    if($error){
        
        header( 'Location: ../addNews.php' ) ;
    }
    else{
        /// clear all $_SESSION variables set above, to prevent a memory leak
        unset($_SESSION['title']);
        unset($_SESSION['date']);
        unset($_SESSION['speaker']);
        unset($_SESSION['room']);
        unset($_SESSION['desc']);

        $title = mysql_real_escape_string( trim($_POST['title']));
        $date_array = split('/', trim($_POST['date'])); 
        
        $date_time = "$date_array[2]-$date_array[0]-$date_array[1]";

        if($_POST['am_pm'] === 'PM'){
            if(12 != $hour){
                $hour += 12;
            }
        }
        else{
            if(12 == $hour){
                $hour = 0;
            }
        }
        
        $date_time = $date_time . " $hour:" . $_POST['minute'] . ":00";

        $category = mysql_real_escape_string( trim($_POST['category']));
        $speaker = mysql_real_escape_string(trim($_POST['speaker']));
        $room = mysql_real_escape_string(trim($_POST['room']));
        $desc = mysql_real_escape_string(trim($_POST['desc']));


        $sql = "INSERT INTO `staff_contact` ( `id` , `title` , `date_time` , `category` , `speaker` , `room` , `description` ) " .               
               "VALUES (Null, '$title', '$date_time', '$category', '$speaker', '$room', '$desc');";

        $result = mysql_query($sql);

        if($result){
             ////TRY saving image file.  Let's use the auto-increment "id" for this event, as that is guaranteed to be unique
             $event_id = mysql_insert_id();
             $uploadfile = "../../saimages/$event_id";
             if  (move_uploaded_file($_FILES['image_data']['tmp_name'], $uploadfile) ) {
                 header( 'Location: ../viewNews.php' ) ;             
             } 
             else {
                 header( 'Location: ../viewNews.php' ) ;             
             }
        }
        else{
            print "<html><body><h3><font color='red'>ERROR:  could not add event!</font></h3></body></html>";
        }
    }

?> 

 

 

And this one handles the updating

 


<?php
	  include("conn.php");
	  $mode=$_GET["mode"];
	  if($mode=="add") {
	  	$first_name=$_POST["first_name"];
		$last_name=$_POST["last_name"];
		$title=$_POST["title"];
		$work_group=$_POST["work_group"];
		$email=$_POST["email"];

		$sql="insert into staff_contact (first_name,
										last_name,
										title,
										work_group,
										email) 
						  values('$first_name',
								 '$last_name',
								 '$title',
								 '$work_group',
								 '$email')";

		$result=mysql_query($sql,$conn) or die(mysql_error());
		header("location: ../viewNews.php");

	  } elseif($mode=="update") {
	  	$id=$_POST["id"];
		$title=$_POST["title"];
		$date_time=$_POST["date_time"];
		$category=$_POST["category"];
		$speaker=$_POST["speaker"];
		$room=$_POST["room"];
		$description=$_POST["description"];
		$image="<img src='../saimages/" . $row['id'] . "' height='100' width='90'>";

		$sql="update staff_contact set id='$id',
									   title='$title',
									   date_time='$date_time',
									   category='$category',
									   speaker='$speaker',
									   room='$room',
									   description='$description'where id='$id'";
		//echo $sql;

		$result=mysql_query($sql,$conn) or die(mysql_error());


		if($result){
             ////TRY saving image file.  Let's use the auto-increment "id" for this event, as that is guaranteed to be unique
             $event_id = mysql_insert_id();
             $uploadfile = "../../saimages/$event_id";
             if  (move_uploaded_file($_FILES['image_data']['tmp_name'], $uploadfile) ) {
                 header( 'Location: ../viewNews.php' ) ;             
             } 
             else {
                 header( 'Location: ../viewNews.php' ) ;             
             }
	  
	  }
?>

 

 

I don't know what's going on.

 

$event_id = mysql_insert_id();
$uploadfile = "../../saimages/$event_id";
if  (move_uploaded_file($_FILES['image_data']['tmp_name'], $uploadfile) ) {
                 header( 'Location: ../viewNews.php' ) ;             
} 

 

check the image after upload, you're not setting an extension;

 

if you upload:

this-image.jpg

put it into the database under id 4

$event_id = 4;

$uploadfile = "../../saimage/4";

there's no extension

Thanks gevans

 

I can read and see the image when I first upload it. But when I want to update it with a new image it does not work, but I can still see the thumbnail of the image I want to replace.

 

The problem is updating the image. Not retrieving it or seeing it.

 

Thank you for your help and interest.

First, I want to thank you for taking time to look at my code. This is the update form

 


<table>
	  <?php
	  $mode=$_GET["mode"];
	  if($mode=="add") {
	  ?>
          <form name="form1" method="post" action="CMS2/newsSubmit.php?mode=add">
          	  <tr>
              	<th colspan="5">Add Contact</th>
              </tr>
              <tr>
                <td>First Name </td>
                <td><input name="first_name" type="text" id="first_name"></td>
              </tr>
              <tr>
                <td>Last Name</td>
                <td><input name="last_name" type="text" id="last_name"></td>
              </tr>
              <tr>
                <td>Title </td>
                <td><textarea name="title" cols="40" rows="10"></textarea></td>
              </tr>
              <tr>
                <td>Work Group </td>
                <td><textarea name="work_group" cols="40" rows="10"></textarea></td>
              </tr>
              <tr>
                <td>Email </td>
                <td><input name="email" type="text" id="email"></td>
              </tr>
              <tr>
                <td><input type="submit" name="Submit" value="Save Data"></td>
                <td></td>
              </tr>
            
          </form>
          </table>
	  <?php		
	  } else  {
	  	include("CMS2/conn.php");
		$id=$_GET["id"];
		$sql="select id,title,date_time,category,speaker,room,description from staff_contact where id='$id'";

		$result=mysql_query($sql,$conn) or die(mysql_error());
		while($row=mysql_fetch_array($result)) {
			$id=$row['id'];
			$title=$row['title'];
			$date_time=$row['date_time'];
			$category=$row['category'];
			$speaker=$row['speaker'];
			$room=$row['room'];
			$description=$row['description'];
			$image="<img src='../saimages/" . $row['id'] . "' height='100' width='90'>";
		}
	?>

        	<h1>Update Contact</h1>
            <table style="width: 720px;">
            <form name="form1" method="post" action="CMS2/newsSubmit.php?mode=update">
            
              <tr>
                <td><input type="hidden" name="id" value="<? echo $id; ?>"></td>
                <td></td>
                <td rowspan="3" align="center"><?php echo $image ?></td>
              </tr>
              <tr>
                <td>Title </td>
                <td><input name="title" type="text" id="title" value="<? echo $title; ?>" size="53"></td>
              </tr>
              <tr>
                <td>Date/Time </td>
                <td> <span class="Available">24-hour clock convention. (2008-12-24 13:00:00)</span><br /><br />
                <input name="date_time" type="text" id="date_time" value="<? echo $date_time; ?>" size="53"></td>
              </tr>
              <tr>
                <td>Category </td>
                <td><input name="category" type="text" id="category" value="<? echo $category; ?>" size="53"></td>
              </tr>
              <tr>
                <td>Speaker </td>
                <td><input name="speaker" type="text" id="speaker" value="<? echo $speaker; ?>" size="53"></td>
              </tr>
              <tr>
                <td>Room </td>
                <td><input name="room" type="text" id="room" value="<? echo $room; ?>" size="53"></td>
              </tr>
              <tr>
                <td>Description </td>
                <td><textarea name="description" cols="40" rows="10"><? echo $description; ?></textarea></td>
              </tr>
              <tr>
              <td> 
               Image File:              </td>
              <td>
               <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />  <!-- image max size is 2 MB = 1024 * 1024 * 2 bytes --> 
               <input name="image_data" type="file" />              </td>
             </tr>
              <tr>
                <td><input type="submit" name="Submit" value="Update Data"></td>
                <td> </td>
              </tr>
             </form>
		</table>
            
          

	<?	

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.